Browse Source

设置session过期时间

master
WendyYang 1 year ago
parent
commit
ea806b6112
4 changed files with 52 additions and 5 deletions
  1. +2
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/App.java
  2. +11
    -5
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java
  3. +38
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java
  4. +1
    -0
      pmapi/src/main/resources/application-dev.yml

+ 2
- 0
pmapi/src/main/java/com/ningdatech/pmapi/App.java View File

@@ -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)


+ 11
- 5
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java View File

@@ -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;

/**
* <p>
* 设置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;
}

}

+ 38
- 0
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionTimeoutConfig.java View File

@@ -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;

/**
* <p>
* 设置session的过期时间为一天
* </p>
*
* @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);
}

}

+ 1
- 0
pmapi/src/main/resources/application-dev.yml View File

@@ -11,6 +11,7 @@ spring:
store-type: redis
redis:
namespace: "spring:session"
timeout: 864000
redis:
timeout: 5000
host: 47.98.125.47


Loading…
Cancel
Save