[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

Spring Boot v2.x or later (Added on 2019/7/18)

How to implement HttpSessionListener

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 {
}

Recommended Posts

Change session timeout time in Spring Boot
Spring Boot --How to set session timeout time
How to change application.properties settings at boot time in Spring boot
Spring Boot 2 multi-project in Gradle
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
SerializationException in Spring Boot (1 series) + spring-security-oauth2 + Redis Session + Heroku
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Spring Boot for the first time
[Rails] Session timeout setting in devise
Write test code in Spring Boot
Store session information in database in Spring Session
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot
Launch (old) Spring Boot project in IntelliJ
Output Spring Boot log in json format
Local file download memorandum in Spring Boot
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Practice! ] Display Hello World in Spring Boot
Use DynamoDB query method in Spring Boot
DI SessionScope Bean in Spring Boot 2 Filter
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Output request and response log in Spring Boot
How to use Spring Boot session attributes (@SessionAttributes)
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
Java tips-Create a Spring Boot project in Gradle
Spring Boot Memorandum
gae + spring boot
Annotations used in Spring Boot task management tool
View the Gradle task in the Spring Boot project
Specify the encoding of static resources in Spring Boot
Include external jar in package with Spring boot2 + Maven3
I checked asynchronous execution of queries in Spring Boot 1.5.9
Change date and time to Japanese notation in Rails
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
Test field-injected class in Spring boot test without using Spring container
Until you start development with Spring Boot in eclipse 1
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)
Until you start development with Spring Boot in eclipse 2
Inject Logger in Spring
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Use Interceptor in Spring
Learning Spring Boot [Beginning]
Microservices in Spring Cloud
Spring boot memo writing (2)