[JAVA] Spring Boot --How to set session timeout time

A memorandum on how to set the session timeout period in Spring Boot.

Environment

--Spring Boot v2.0.3 (* Spring Session is unused)

Setting method

For JAR builds

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

For WAR build

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.

How to set in web.xml

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.

How to set with HttpSessionListener

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

Spring Boot --How to set session timeout time
How to set Spring Boot + PostgreSQL
Change session timeout time in Spring Boot
How to use Spring Boot session attributes (@SessionAttributes)
How to set Dependency Injection (DI) for Spring Boot
How to use ModelMapper (Spring boot)
How to change application.properties settings at boot time in Spring boot
Read H2 database for unit tests
How to create a database for H2 Database anywhere
Spring Boot --How to set session timeout time
[Java] How to set the Date time to 00:00:00
How to split Spring Boot message file
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to make Spring Boot Docker Image smaller
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
How to set environment variables in the properties file of Spring boot application
How to set the display time to Japan time in Rails
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to boot by environment with Spring Boot of Maven
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
How to set Java constants
Set cookies with Spring Boot
Introduction to Spring Boot Part 1
How to set chrony when the time shifts in CentOS7
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL
How to use Lombok in Spring
How to unit test Spring AOP
Spring Boot for the first time
How to use Spring Data JDBC
[How to install Spring Data Jpa]
Upgrade spring boot from 1.5 series to 2.0 series
How to set Lombok in Eclipse
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to create your own Controller corresponding to / error with Spring Boot
How to set and use profile in annotation-based Configuration in Spring framework
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
Set Spring Security authentication result to JSON
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
[Introduction to Spring Boot] Form validation check
[Spring Boot] I investigated how to implement post-processing of the received request.
How to read Body of Request multiple times with Spring Boot + Spring Security
How to include Spring Tool in Eclipse 4.6.3?
Story when moving from Spring Boot 1.5 to 2.1
[Spring Boot Actuator] How to manually register your own health check process
How to set JAVA_HOME with Maven appassembler-maven-plugin
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
How to not start Flyway when running unit tests in Spring Boot
[Spring MVC] How to pass path variables
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Add spring boot and gradle to eclipse
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
Part2 Part II. How to proceed with Getting Started Spring Boot Reference Guide Note ①
[Reverse lookup] Spring Security (updated from time to time)
Understand how to share Spring DB connection (DB transaction)
Set the time of LocalDateTime to a specific time