1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

Fix WebServerSessionConfigure

This commit is contained in:
dengchao@xgtl 2020-04-17 16:37:50 +08:00
parent 2b4b908afa
commit 3161fca335
2 changed files with 37 additions and 30 deletions

View File

@ -22,13 +22,18 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.RedisFlushMode; import org.springframework.lang.NonNull;
import org.springframework.session.data.redis.RedisOperationsSessionRepository; import org.springframework.session.FlushMode;
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import java.time.Duration;
/** /**
* maxInactiveIntervalInSeconds: 设置 Session 失效时间 * maxInactiveIntervalInSeconds: 设置 Session 失效时间
@ -68,26 +73,20 @@ public class WebServerSessionConfigure {
@Primary @Primary
@Bean @Bean
public RedisOperationsSessionRepository sessionRepository(RedisTemplate<Object, Object> sessionRedisTemplate) { public RedisIndexedSessionRepository redisIndexedSessionRepository(@NonNull RedisTemplate<Object, Object> sessionRedisTemplate) {
RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(sessionRedisTemplate); RedisIndexedSessionRepository repository = new RedisIndexedSessionRepository(sessionRedisTemplate);
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds); repository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
sessionRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); repository.setFlushMode(FlushMode.IMMEDIATE);
sessionRepository.setRedisKeyNamespace(RedisKey.CACHE_SESSIONS); repository.setRedisKeyNamespace(RedisKey.CACHE_SESSIONS);
return sessionRepository; return repository;
} }
@Bean @Bean
public RedisTemplate<Object, Object> sessionRedisTemplate() { public RedisTemplate<Object, Object> sessionRedisTemplate() {
JedisConnectionFactory factory = new JedisConnectionFactory(); JedisConnectionFactory factory = createJedisConnectionFactory(sessionDb);
factory.setHostName(host);
factory.setPort(port);
factory.setDatabase(sessionDb);
if (StringUtils.isNotBlank(pass)) {
factory.setPassword(pass);
}
factory.setTimeout(timeout);
factory.afterPropertiesSet(); factory.afterPropertiesSet();
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(factory); template.setConnectionFactory(factory);
@ -97,18 +96,10 @@ public class WebServerSessionConfigure {
/** /**
* 存储AuthToken * 存储AuthToken
* @return
*/ */
@Bean @Bean
public AuthRedisTemplate authRedisTemplate() { public AuthRedisTemplate authRedisTemplate() {
JedisConnectionFactory factory = new JedisConnectionFactory(); JedisConnectionFactory factory = createJedisConnectionFactory(tokenDb);
factory.setHostName(host);
factory.setPort(port);
factory.setDatabase(tokenDb);
if (StringUtils.isNotBlank(pass)) {
factory.setPassword(pass);
}
factory.setTimeout(timeout);
factory.afterPropertiesSet(); factory.afterPropertiesSet();
AuthRedisTemplate template = new AuthRedisTemplate(); AuthRedisTemplate template = new AuthRedisTemplate();
@ -118,4 +109,20 @@ public class WebServerSessionConfigure {
return template; return template;
} }
@NonNull
private JedisConnectionFactory createJedisConnectionFactory(int tokenDb) {
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(host);
standaloneConfiguration.setDatabase(tokenDb);
standaloneConfiguration.setPort(port);
if (StringUtils.isNotBlank(pass)) {
standaloneConfiguration.setPassword(pass);
}
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofMillis(timeout))
.readTimeout(Duration.ofMillis(timeout))
.build();
return new JedisConnectionFactory(standaloneConfiguration, jedisClientConfiguration);
}
} }

View File

@ -109,13 +109,13 @@ spring.redis.port=6379
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 # Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
spring.redis.password= spring.redis.password=
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 # \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.lettuce.pool.max-active=-1 spring.redis.jedis.pool.max-active=-1
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 # \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.lettuce.pool.max-wait=-1 spring.redis.jedis.pool.max-wait=-1
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5
spring.redis.lettuce.pool.max-idle=20 spring.redis.jedis.pool.max-idle=20
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5
spring.redis.lettuce.pool.min-idle=10 spring.redis.jedis.pool.min-idle=10
# \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09\uFF0C\u56E0\u4E3A\u6625\u677E\u5BA2\u670D\u542F\u52A8\u65F6 # \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09\uFF0C\u56E0\u4E3A\u6625\u677E\u5BA2\u670D\u542F\u52A8\u65F6
# \u5360\u7528\u6BD4\u8F83\u5927\u7684\u65F6\u95F4\uFF0C\u6B64\u5904timeout\u503C\u5FC5\u987B\u8BBE\u7F6E\u4E3A0\u624D\u80FD\u6B63\u5E38\u542F\u52A8 # \u5360\u7528\u6BD4\u8F83\u5927\u7684\u65F6\u95F4\uFF0C\u6B64\u5904timeout\u503C\u5FC5\u987B\u8BBE\u7F6E\u4E3A0\u624D\u80FD\u6B63\u5E38\u542F\u52A8
spring.redis.timeout=0 spring.redis.timeout=0