From 1b3740a99aae8a89b268491d78e95147b7ddbabb Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 27 Nov 2019 11:48:52 +0800 Subject: [PATCH] 2.5.6 --- .../common/MyApiSessionManager.java | 58 +++++++++++++++++++ .../sop/adminserver/config/WebConfig.java | 3 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/common/MyApiSessionManager.java diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/common/MyApiSessionManager.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/common/MyApiSessionManager.java new file mode 100644 index 00000000..4a0dcd27 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/common/MyApiSessionManager.java @@ -0,0 +1,58 @@ +package com.gitee.sop.adminserver.common; + +import com.gitee.easyopen.exception.ApiException; +import com.gitee.easyopen.session.ApiSessionManager; +import com.google.common.cache.LoadingCache; +import lombok.extern.slf4j.Slf4j; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSession; + +/** + * @author tanghc + */ +@Slf4j +public class MyApiSessionManager extends ApiSessionManager { + + private volatile LoadingCache cache; + + @Override + public HttpSession getSession(String sessionId) { + if (sessionId == null) { + return this.createSession(sessionId); + } + try { + return getCache().get(sessionId); + } catch (Exception e) { + log.error(e.getMessage(), e); + throw new ApiException("create session error"); + } + } + + /** + * 创建一个session + * + * @param sessionId 传null将返回一个新session + * @return 返回session + */ + @Override + protected HttpSession createSession(String sessionId) { + ServletContext servletContext = getServletContext(); + HttpSession session = this.newSession(sessionId, servletContext); + session.setMaxInactiveInterval(getSessionTimeout()); + getCache().put(session.getId(), session); + return session; + } + + public LoadingCache getCache() { + if (cache == null) { + synchronized (ApiSessionManager.class) { + if (cache == null) { + cache = buildCache(); + } + } + } + return cache; + } + +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java index 2284a716..4ca883fc 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java @@ -8,6 +8,7 @@ import com.gitee.easyopen.ApiParamParser; import com.gitee.easyopen.ParamNames; import com.gitee.easyopen.interceptor.ApiInterceptor; import com.gitee.easyopen.session.ApiSessionManager; +import com.gitee.sop.adminserver.common.MyApiSessionManager; import com.gitee.sop.adminserver.interceptor.LoginInterceptor; import com.gitee.sop.adminserver.service.RegistryService; import com.gitee.sop.adminserver.service.impl.RegistryServiceEurekaImpl; @@ -48,7 +49,7 @@ public class WebConfig { , SerializerFeature.WriteDateUseDateFormat) ); - ApiSessionManager apiSessionManager = new ApiSessionManager(); + ApiSessionManager apiSessionManager = new MyApiSessionManager(); // session有效期 int timeout = NumberUtils.toInt(accessTokenTimeout, 30); apiSessionManager.setSessionTimeout(timeout);