Spring Boot session timeout

前端 未结 7 2113
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 06:28

server.session-timeout seems to be working only for embedded tomcat.

I put a log statement to check the session max interval time. After deploying the

7条回答
  •  情深已故
    2020-12-31 07:14

    Complementing the @Ali answer, you can also create a session.timeout variable in your application.yml file and use it in your class. This should work great with Spring Boot war and external Tomcat:

    application.yml

      session:
        timeout: 480 # minutes
    

    SessionListener (with @Configuration annotation)

    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    class SessionListener implements HttpSessionListener {
    
        @Value("${session.timeout}")
        private Integer sessionTimeout;
    
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            event.getSession().setMaxInactiveInterval(sessionTimeout);
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent event) {}
    
    }
    

提交回复
热议问题