[JAVA] Change session timeout time in Spring Boot
The story of changing the session timeout time in Spring Boot
environment
Spring Boot v1.5.1.RELEASE
and
Spring Boot v2.1.5.RELEASE (Added on 2019/7/18)
Modification method
Edit application.properties (or .yml file)
Any of the following can be changed without changing the module, but the process when creating a session or destroying a session cannot be written.
Before Spring Boot v2.x
- Set the
server.session.timeout
property to any number of seconds
- Reference URL: https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/common-application-properties.html
Spring Boot v2.x or later (Added on 2019/7/18)
- Set the value to the
server.servlet.session.timeout
property
- Reference URL: https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/common-application-properties.html
- From the above document, it seems that you can specify in minutes or seconds by adding a suffix (m = minutes, s = seconds) to the numerical value.
- Specifying a value less than 60 seconds does not result in less than 60 seconds (minimum 1 minute)
- Session timeout time is finally managed in minutes, it seems that less than 60 seconds will be truncated
- Reference URL1: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/ tomcat / tomcatServletWebServerFactory.java
- Reference URL2: http://k6i.hateblo.jp/entry/2018/07/14/173704
How to implement HttpSessionListener
- Create a class that implements HttpSessionListener
- When creating a session, you can write the processing you want to do when destroying the session
- Is it redundant if you just want to change the session timeout time?
MySessionListener.java
public class MySessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
//Set timeout time
HttpSession session = se.getSession();
session.setMaxInactiveInterval(1800); //Specify the number of timeout seconds. Negative number does not time out session
//Processing when creating a session
// ...
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
//Processing when session is destroyed
// ...
}
WebAppConfig.java
@Configuration
@Import({MySessionListener.class}) //Import and activate
public class WebAppConfig {
}