Application property “server.servlet.session.timeout” is not working in Spring Boot project

后端 未结 6 1828
南方客
南方客 2020-12-11 15:55

According to the documentation of Spring Boot, session timeout can be configured by setting

server.servlet.session.timeout= 300s

in

相关标签:
6条回答
  • 2020-12-11 16:24

    Use HttpSessionListener.

    server.servlet.session.timeout only works for embedded container.

    @Configuration
    public class MyHttpSessionListener implements HttpSessionListener {
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            event.getSession().setMaxInactiveInterval(30);
        }
    }
    
    0 讨论(0)
  • 2020-12-11 16:26

    Maybe you added remember me. This will make the session always valid.

    0 讨论(0)
  • 2020-12-11 16:28

    spring doc The latest version of SpringBoot is using the following properties.

    server.servlet.session.timeout=30m
    
    0 讨论(0)
  • 2020-12-11 16:34

    You can use Approach 1:

    server.servlet.session.timeout=30s
    server.servlet.session.cookie.max-age=30s
    

    It is working fine for me

    0 讨论(0)
  • 2020-12-11 16:35

    I am posting answer because this scenario is new for me. And I haven't got proper solution step by step. According to the suggestion of M. Deinum I created a web.xml file under WEB-INF folder. Project structure is like

    src
     |_ main
         |_ java
         |_ resources
         |_ webapp
             |_ WEB-INF
                  |_ web.xml
    

    And in web.xml I configured <session-timeout>...</session-timeout>

    My web.xml is like

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             id="WebApp_ID" version="2.5">
    
    
        <session-config>
            <session-timeout>5</session-timeout>
        </session-config>
    
    </web-app>
    

    And now session time of my webapp in server is working according to my configuration. Thanks goes to M. Deinum

    0 讨论(0)
  • 2020-12-11 16:35

    A possible cause for this problem might be using @EnableRedisHttpSession. As explained in this answer:

    By using @EnableRedisHttpSession you are telling Spring Boot that you want to take complete control over the configuration of Redis-based HTTP sessions. As a result, its auto-configuration backs off and server.servlet.session.timeout has no effect. If you want to use server.servlet.session.timeout then you should remove @EnableRedisHttpSession. Alternatively, if you want to use @EnableRedisHttpSession then you should use the maxInactiveIntervalInSeconds attribute to configure the session timeout.

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题