From ea806b6112736722c201896f226076930bfea604 Mon Sep 17 00:00:00 2001 From: WendyYang Date: Thu, 15 Jun 2023 17:13:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=AE=BE=E7=BD=AEsession=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmapi/src/main/java/com/ningdatech/pmapi/App.java | 2 ++ .../security/auth/config/RedisSessionConfig.java | 16 ++++++--- .../auth/config/RedisSessionTimeoutConfig.java | 38 ++++++++++++++++++++++ pmapi/src/main/resources/application-dev.yml | 1 + 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/App.java b/pmapi/src/main/java/com/ningdatech/pmapi/App.java index e231688..f667f5f 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/App.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/App.java @@ -10,11 +10,13 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @author liuxinxin */ +@EnableRedisHttpSession @SpringBootApplication @EnableAsync @MapperScan(App.MAPPER_PACKAGES) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java index 530d12e..3c6daed 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java @@ -2,12 +2,17 @@ package com.ningdatech.pmapi.user.security.auth.config;//package com.ningdatech. import com.ningdatech.pmapi.common.constant.BizConst; import com.ningdatech.pmapi.user.security.auth.constants.SessionTimeConstant; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; +import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; import org.springframework.session.web.http.CookieHttpSessionIdResolver; import org.springframework.session.web.http.DefaultCookieSerializer; +import javax.annotation.PostConstruct; + /** *

* 设置session的过期时间为一天 @@ -17,10 +22,9 @@ import org.springframework.session.web.http.DefaultCookieSerializer; * @Date 2020/7/29 9:46 上午 * @Version 1.0 **/ - @Configuration -// 设置session的过期时间为一天 -@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60 * 10) +@RequiredArgsConstructor +@AutoConfigureAfter(RedisHttpSessionConfiguration.class) public class RedisSessionConfig { @Bean @@ -30,9 +34,11 @@ public class RedisSessionConfig { // 创建 DefaultCookieSerializer 对象 DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer(); - sessionIdResolver.setCookieSerializer(cookieSerializer); // 设置到 sessionIdResolver 中 + sessionIdResolver.setCookieSerializer(cookieSerializer); + // 设置到 sessionIdResolver 中 cookieSerializer.setCookieName(BizConst.COOKIE_KEY); cookieSerializer.setCookieMaxAge(SessionTimeConstant.SESSION_TIME_SECONDS); return sessionIdResolver; } + } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java new file mode 100644 index 0000000..99676cf --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java @@ -0,0 +1,38 @@ +package com.ningdatech.pmapi.user.security.auth.config; + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.context.annotation.Configuration; +import org.springframework.session.data.redis.RedisIndexedSessionRepository; +import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; + +import javax.annotation.PostConstruct; + +/** + *

+ * 设置session的过期时间为一天 + *

+ * + * @Author LiuXinXin + * @Date 2020/7/29 9:46 上午 + * @Version 1.0 + **/ +@Configuration +@RequiredArgsConstructor +@AutoConfigureAfter(RedisHttpSessionConfiguration.class) +public class RedisSessionTimeoutConfig { + + @Value("${spring.session.timeout:60 * 60 * 4}") + private Integer springSessionTimeout; + + private final RedisHttpSessionConfiguration redisHttpSessionConfiguration; + private final RedisIndexedSessionRepository redisIndexedSessionRepository; + + @PostConstruct + public void sessionTimeoutTime() { + redisHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(springSessionTimeout); + redisIndexedSessionRepository.setDefaultMaxInactiveInterval(springSessionTimeout); + } + +} diff --git a/pmapi/src/main/resources/application-dev.yml b/pmapi/src/main/resources/application-dev.yml index e95eb4f..6820c94 100644 --- a/pmapi/src/main/resources/application-dev.yml +++ b/pmapi/src/main/resources/application-dev.yml @@ -11,6 +11,7 @@ spring: store-type: redis redis: namespace: "spring:session" + timeout: 864000 redis: timeout: 5000 host: 47.98.125.47 From c04f12d4bb9b6e1507d7a9cbd9665c9f3eba7635 Mon Sep 17 00:00:00 2001 From: WendyYang Date: Thu, 15 Jun 2023 19:24:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=AE=BE=E7=BD=AEsession=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmapi/src/main/java/com/ningdatech/pmapi/App.java | 2 -- .../pmapi/user/security/auth/config/RedisSessionConfig.java | 10 ++-------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/App.java b/pmapi/src/main/java/com/ningdatech/pmapi/App.java index f667f5f..e231688 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/App.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/App.java @@ -10,13 +10,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @author liuxinxin */ -@EnableRedisHttpSession @SpringBootApplication @EnableAsync @MapperScan(App.MAPPER_PACKAGES) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java index 3c6daed..1eaec87 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java @@ -2,17 +2,12 @@ package com.ningdatech.pmapi.user.security.auth.config;//package com.ningdatech. import com.ningdatech.pmapi.common.constant.BizConst; import com.ningdatech.pmapi.user.security.auth.constants.SessionTimeConstant; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.web.http.CookieHttpSessionIdResolver; import org.springframework.session.web.http.DefaultCookieSerializer; -import javax.annotation.PostConstruct; - /** *

* 设置session的过期时间为一天 @@ -23,8 +18,7 @@ import javax.annotation.PostConstruct; * @Version 1.0 **/ @Configuration -@RequiredArgsConstructor -@AutoConfigureAfter(RedisHttpSessionConfiguration.class) +@EnableRedisHttpSession public class RedisSessionConfig { @Bean