@EnableRedisHttpSession + Spring Boot ignoring server.session.timeout on application.yml

Fernando Barbeiro picture Fernando Barbeiro · May 18, 2016 · Viewed 12k times · Source

I have a project with Spring Boot 1.3.3 [another stuff] and Redis configurated to manage sessions, i.e., @EnableRedisHttpSession. The application works well and stores the information on Redis regularly. The problem that I'm facing is that, different from what documentation says, whether I define or not a server.session.timeout, the Redis always is using the default value for its annotation attribute (maxInactiveIntervalInSeconds) that is: 1800

Here, the documentation that I followed: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-session.html

I've also tried the approach defined by @rwinch here https://github.com/spring-projects/spring-session/issues/110 but also without success.

Updating ......


My configuration file as requested:

#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

Beside that, I've also tried to implement a SessionListener that was in charge of setting the timeout (something like this):

    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);
            }
        }
...

It still didn't result in a correct scenario. I'm really racking my brain :|


Please guys, am I missing some point? Does anyone else have faced it?

Thanks in advance.

Answer

alejandropg picture alejandropg · Sep 20, 2016

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.