@EnableRedisHttpSession + Spring 引导忽略 server.session.timeout on application.yml
@EnableRedisHttpSession + Spring Boot ignoring server.session.timeout on application.yml
我有一个项目 Spring Boot 1.3.3 [another stuff] 和 Redis 配置为管理会话,即,@EnableRedisHttpSession。该应用程序运行良好,并定期将信息存储在 Redis 上。
我面临的问题是,与文档所说的不同,无论我是否定义 server.session.timeout,Redis 始终为其注释属性使用默认值(maxInactiveIntervalInSeconds) 即:1800
这里是我遵循的文档:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-session.html
我也在此处尝试了@rwinch 定义的方法 https://github.com/spring-projects/spring-session/issues/110 但也没有成功。
更新中......
我要求的配置文件:
#First attempt (server.session.timeout) following the Spring documentation mentioned
server:
session:
timeout: 10
spring:
#session timeout under spring (as mentioned by M Deinum in comment - unfortunately doesnt work)
session:
timeout: 10
redis:
host: 192.168.99.101
port: 6379
除此之外,我还尝试实现一个负责设置超时的 SessionListener(类似这样):
public class SessionListener implements HttpSessionListener {
@Value(value = "${server.session.timeout}")
private int timeout;
@Override
public void sessionCreated(HttpSessionEvent event) {
if(event!=null && event.getSession()!=null){
event.getSession().setMaxInactiveInterval(timeout);
}
}
...
它仍然没有产生正确的场景。真是绞尽脑汁:|
拜托各位,我是不是漏掉了什么?还有其他人遇到过吗?
提前致谢。
好吧,以防万一有人遇到同样的情况,我们有两种解决方法:
我。执行以下操作:
@EnableRedisHttpSession
public class Application {
//some other codes here
@Value("${spring.session.timeout}")
private Integer maxInactiveIntervalInSeconds;
@Bean
public RedisOperationsSessionRepository sessionRepository( RedisConnectionFactory factory) {
RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(factory);
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
return sessionRepository;
}
不幸的是,我不得不实现一个侦听器以便在会话过期时执行其他操作。而且,当您定义 RedisOperationsSessionRepository 时,您不再有 HttpSessionListener(取而代之的是 SessionMessageListener,如此处所述:http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository)。由于这个问题,需要第二种方法。
二.克服问题:
@EnableRedisHttpSession
public class Application implements ApplicationListener{
@Value("${spring.session.timeout}")
private Integer maxInactiveIntervalInSeconds;
@Autowired
private RedisOperationsSessionRepository redisOperation;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
redisOperation.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
}
}
...
假设其中 none 个是理想的开箱即用设置,至少它们允许我继续我的 PoC。
另一个解决方案:
@EnableRedisHttpSession
public class HttpSessionConfig {
@Value("${server.session.timeout}")
private Integer maxInactiveIntervalInMinutes;
@Inject
private RedisOperationsSessionRepository sessionRepository;
@PostConstruct
private void afterPropertiesSet() {
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInMinutes * 60);
}
在这种情况下,您使用默认配置,只需添加您的超时时间。所以你维护默认的HttpSessionListener,你不需要使用ApplicationListener来设置超时,只需要在应用程序生命周期中设置一次。
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)
扩展 RedisHttpSessionConfiguration
并在 @PostConstruct
方法中执行初始化。
@Configuration
public class HttpSessionConfig extends RedisHttpSessionConfiguration {
@Value("${spring.session.timeout}")
private Integer sessionTimeoutInSec;
@Value("${spring.session.redis.namespace}")
private String sessionRedisNamespace;
@Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
@PostConstruct
public void initConfig() throws Exception {
this.setMaxInactiveIntervalInSeconds(sessionTimeoutInSec);
this.setRedisNamespace(sessionRedisNamespace);
}
}
您可以删除 EnableRedisHttpSession
注释,而是设置 属性:
spring.session.store-type=redis
spring.session.timeout
和 server.servlet.session.timeout
都可以。请注意 spring.session.timeout
将根据我的测试覆盖 server.servlet.session.timeout
。
我有一个项目 Spring Boot 1.3.3 [another stuff] 和 Redis 配置为管理会话,即,@EnableRedisHttpSession。该应用程序运行良好,并定期将信息存储在 Redis 上。 我面临的问题是,与文档所说的不同,无论我是否定义 server.session.timeout,Redis 始终为其注释属性使用默认值(maxInactiveIntervalInSeconds) 即:1800
这里是我遵循的文档:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-session.html
我也在此处尝试了@rwinch 定义的方法 https://github.com/spring-projects/spring-session/issues/110 但也没有成功。
更新中......
我要求的配置文件:
#First attempt (server.session.timeout) following the Spring documentation mentioned
server:
session:
timeout: 10
spring:
#session timeout under spring (as mentioned by M Deinum in comment - unfortunately doesnt work)
session:
timeout: 10
redis:
host: 192.168.99.101
port: 6379
除此之外,我还尝试实现一个负责设置超时的 SessionListener(类似这样):
public class SessionListener implements HttpSessionListener {
@Value(value = "${server.session.timeout}")
private int timeout;
@Override
public void sessionCreated(HttpSessionEvent event) {
if(event!=null && event.getSession()!=null){
event.getSession().setMaxInactiveInterval(timeout);
}
}
...
它仍然没有产生正确的场景。真是绞尽脑汁:|
拜托各位,我是不是漏掉了什么?还有其他人遇到过吗?
提前致谢。
好吧,以防万一有人遇到同样的情况,我们有两种解决方法:
我。执行以下操作:
@EnableRedisHttpSession
public class Application {
//some other codes here
@Value("${spring.session.timeout}")
private Integer maxInactiveIntervalInSeconds;
@Bean
public RedisOperationsSessionRepository sessionRepository( RedisConnectionFactory factory) {
RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(factory);
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
return sessionRepository;
}
不幸的是,我不得不实现一个侦听器以便在会话过期时执行其他操作。而且,当您定义 RedisOperationsSessionRepository 时,您不再有 HttpSessionListener(取而代之的是 SessionMessageListener,如此处所述:http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository)。由于这个问题,需要第二种方法。
二.克服问题:
@EnableRedisHttpSession
public class Application implements ApplicationListener{
@Value("${spring.session.timeout}")
private Integer maxInactiveIntervalInSeconds;
@Autowired
private RedisOperationsSessionRepository redisOperation;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
redisOperation.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
}
}
...
假设其中 none 个是理想的开箱即用设置,至少它们允许我继续我的 PoC。
另一个解决方案:
@EnableRedisHttpSession
public class HttpSessionConfig {
@Value("${server.session.timeout}")
private Integer maxInactiveIntervalInMinutes;
@Inject
private RedisOperationsSessionRepository sessionRepository;
@PostConstruct
private void afterPropertiesSet() {
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInMinutes * 60);
}
在这种情况下,您使用默认配置,只需添加您的超时时间。所以你维护默认的HttpSessionListener,你不需要使用ApplicationListener来设置超时,只需要在应用程序生命周期中设置一次。
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)
扩展 RedisHttpSessionConfiguration
并在 @PostConstruct
方法中执行初始化。
@Configuration
public class HttpSessionConfig extends RedisHttpSessionConfiguration {
@Value("${spring.session.timeout}")
private Integer sessionTimeoutInSec;
@Value("${spring.session.redis.namespace}")
private String sessionRedisNamespace;
@Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
@PostConstruct
public void initConfig() throws Exception {
this.setMaxInactiveIntervalInSeconds(sessionTimeoutInSec);
this.setRedisNamespace(sessionRedisNamespace);
}
}
您可以删除 EnableRedisHttpSession
注释,而是设置 属性:
spring.session.store-type=redis
spring.session.timeout
和 server.servlet.session.timeout
都可以。请注意 spring.session.timeout
将根据我的测试覆盖 server.servlet.session.timeout
。