From 1f09dbb24a5bb60d58f152109fc7a73863a889dc Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 22 Jul 2020 13:57:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4session=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4=E4=B8=BA=E5=8F=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E9=BB=98=E8=AE=A41800s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/metersphere/config/ShiroConfig.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/io/metersphere/config/ShiroConfig.java b/backend/src/main/java/io/metersphere/config/ShiroConfig.java index 92d723b810..f668f89d8e 100644 --- a/backend/src/main/java/io/metersphere/config/ShiroConfig.java +++ b/backend/src/main/java/io/metersphere/config/ShiroConfig.java @@ -14,11 +14,13 @@ import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.ApplicationContext; +import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; +import org.springframework.core.env.Environment; import javax.servlet.DispatcherType; import javax.servlet.Filter; @@ -27,7 +29,8 @@ import java.util.Map; import java.util.Objects; @Configuration -public class ShiroConfig { +public class ShiroConfig implements EnvironmentAware { + private Environment env; @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager sessionManager) { @@ -115,9 +118,11 @@ public class ShiroConfig { @Bean public SessionManager sessionManager(MemoryConstrainedCacheManager memoryConstrainedCacheManager) { + Long sessionTimeout = env.getProperty("session.timeout", Long.class, 1800L); // 默认1800s, 半个小时 + DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionIdUrlRewritingEnabled(false); - sessionManager.setGlobalSessionTimeout(1800000L); + sessionManager.setGlobalSessionTimeout(sessionTimeout * 1000);// 超时时间ms sessionManager.setDeleteInvalidSessions(true); sessionManager.setSessionValidationSchedulerEnabled(true); SimpleCookie sessionIdCookie = new SimpleCookie(); @@ -137,4 +142,9 @@ public class ShiroConfig { ShiroDBRealm shiroDBRealm = (ShiroDBRealm) context.getBean("shiroDBRealm"); ((DefaultWebSecurityManager) context.getBean("securityManager")).setRealm(shiroDBRealm); } + + @Override + public void setEnvironment(Environment environment) { + this.env = environment; + } }