Parcourir la source

登陆 session会话暂存于redis 中

tags/24080901
niohe·erbao il y a 1 an
Parent
révision
06f1237b9d
2 fichiers modifiés avec 42 ajouts et 48 suppressions
  1. +5
    -0
      pmapi/pom.xml
  2. +37
    -48
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java

+ 5
- 0
pmapi/pom.xml Voir le fichier

@@ -39,6 +39,11 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

<!--spring boot 集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>


+ 37
- 48
pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/config/RedisSessionConfig.java Voir le fichier

@@ -1,49 +1,38 @@
package com.ningdatech.pmapi.user.security.auth.config;//package com.ningdatech.pmapi.user.security.auth.config;
//
//import com.ningdatech.basic.util.StrPool;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
///**
// * <p>
// * 设置session的过期时间为一天
// * </p>
// *
// * @Author LiuXinXin
// * @Date 2020/7/29 9:46 上午
// * @Version 1.0
// **/
//@Configuration
//@EnableRedisHttpSession(
// maxInactiveIntervalInSeconds = RedisSessionConfig.SESSION_TIMEOUT,
// redisNamespace = RedisSessionConfig.REDIS_NAMESPACE
//)
//public class RedisSessionConfig {
//
// static final int SESSION_TIMEOUT = 24 * 60 * 60 * 10;
//
// static final String REDIS_NAMESPACE = "#{redisSessionConfig.getRedisNamespace()}";
//
// @Value("${nd.cache.def.keyPrefix:}")
// private String keyPrefix;
//
// public String getRedisNamespace() {
// return (StrUtils.isBlank(keyPrefix) ? StrPool.EMPTY : keyPrefix + StrPool.COLON) + RedisIndexedSessionRepository.DEFAULT_NAMESPACE;
// }
//
// @Bean
// public CookieHttpSessionIdResolver sessionIdResolver() {
// // 创建 CookieHttpSessionIdResolver 对象
// CookieHttpSessionIdResolver sessionIdResolver = new CookieHttpSessionIdResolver();
// // 创建 DefaultCookieSerializer 对象
// DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
//
// // 设置到 sessionIdResolver 中
// sessionIdResolver.setCookieSerializer(cookieSerializer);
// cookieSerializer.setCookieName(BizConst.COOKIE_KEY);
// cookieSerializer.setCookieMaxAge(SessionTimeConstant.SESSION_TIME_SECONDS);
// return sessionIdResolver;
// }
//
//}

import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.user.security.auth.constants.SessionTimeConstant;
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.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
* <p>
* 设置session的过期时间为一天
* </p>
*
* @Author LiuXinXin
* @Date 2020/7/29 9:46 上午
* @Version 1.0
**/

@Configuration
// 设置session的过期时间为一天
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60 * 10)
public class RedisSessionConfig {

@Bean
public CookieHttpSessionIdResolver sessionIdResolver() {
// 创建 CookieHttpSessionIdResolver 对象
CookieHttpSessionIdResolver sessionIdResolver = new CookieHttpSessionIdResolver();

// 创建 DefaultCookieSerializer 对象
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
sessionIdResolver.setCookieSerializer(cookieSerializer); // 设置到 sessionIdResolver 中
cookieSerializer.setCookieName(BizConst.COOKIE_KEY);
cookieSerializer.setCookieMaxAge(SessionTimeConstant.SESSION_TIME_SECONDS);
return sessionIdResolver;
}
}

Chargement…
Annuler
Enregistrer