A memorandum on how to set the session timeout period in Spring Boot.
--Spring Boot v2.0.3 (* Spring Session is unused)
Just add the following settings to application.properties. By the way, the settings under server.servlet seem to be the settings for the built-in application server.
application.properties
server.servlet.session.timeout=30
The settings under server.servlet in application.properties are settings for the built-in application server, so they are not used for war deployment. In case of war deployment, it can be set by either of the following methods.
Create and configure web.xml.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
It is possible to set the default session timeout for the application server in TOMCAT_HOME / conf / web.xml. However, in most cases you will want to set the session timeout for each application. In that case, create src / main / webapp / WEB-INF / web.xml in the application project.
By the way, if you are using eclipse (STS), if you add / src / main / webapp in the "Deployment Assembly" of the project properties, you can create the web when you start the application with WTP. Happy using .xml.
Although it is a primitive method, there is also a method to set with HttpSessionListener, which is executed when Session is created / destroyed. However, if you just want to set the session timeout time, it seems redundant, so I think it is better to specify it in web.xml.
HttpSessionListenerImpl.java
public class HttpSessionListenerImpl implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
//Set session timeout time in seconds
session.setMaxInactiveInterval(1800);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
}
WebAppConfig.java
@Configuration
@Import({HttpSessionListenerImpl.class})
public class WebAppConfig {
// ...
}
Recommended Posts