Fer*_*pos 10 spring-boot spring-session
我有一个项目,春节引导1.3.3 [其他的东西]和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
Run Code Online (Sandbox Code Playgroud)
除此之外,我还尝试实现一个负责设置超时的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);
}
}
...
Run Code Online (Sandbox Code Playgroud)
它仍然没有导致正确的情况.我真的在绞尽脑汁:|
拜托,伙计们,我错过了一些观点吗?有没有其他人面对过它?
提前致谢.
另一个解决方案:
@EnableRedisHttpSession
public class HttpSessionConfig {
@Value("${server.session.timeout}")
private Integer maxInactiveIntervalInMinutes;
@Inject
private RedisOperationsSessionRepository sessionRepository;
@PostConstruct
private void afterPropertiesSet() {
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInMinutes * 60);
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以使用默认配置,而只需添加超时。因此,您将维护默认的HttpSessionListener,并且无需使用ApplicationListener来设置应用程序生命周期中的超时时间(仅一次)。
好吧,万一有人面临同样的情况,我们有两种解决方法:
一、落实以下内容:
@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;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我必须实现一个监听器,以便在会话过期时执行其他操作。而且,当您定义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);
}
}
...
Run Code Online (Sandbox Code Playgroud)
假设它们都不是理想的开箱即用设置,至少它们允许我继续我的 PoC。
| 归档时间: |
|
| 查看次数: |
9188 次 |
| 最近记录: |