@EnableRedisHttpSession + Spring Boot ignoring server.session.timeout on application.yml
Another solution:
@EnableRedisHttpSession
public class HttpSessionConfig {
@Value("${server.session.timeout}")
private Integer maxInactiveIntervalInMinutes;
@Inject
private RedisOperationsSessionRepository sessionRepository;
@PostConstruct
private void afterPropertiesSet() {
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInMinutes * 60);
}
In this way you use the default configuration, and just add your timeout. So you maintain the default HttpSessionListener, and you don't need to use an ApplicationListener to set the time out, just one time, in the application lifecycle.
Well, just in case someone is facing the same situation, we have 2 ways to workaround:
I. Implement the following:
@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;
}
Unfortunately, I had to implement a listener in order to perform additional actions when a session expires. And, when you define a RedisOperationsSessionRepository, you don't have a HttpSessionListener anymore (instead of it, you have a SessionMessageListener, as described here: http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository). Because of this question, the 2nd approach was required.
II. To overcome the problem:
@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);
}
}
...
Assuming that none of them are the desirable out-of-box setup, at least they allow me to continue in my PoC.
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)