Enable session persistence with Spring Boot and embedded Tomcat

According to the Spring this will be fixed in 1.3.0.M2 and eventually in 1.3.0.RELEASE

Then all you got to do is add the following line to your application.properties file.

server.session.persistent=true

In recent Spring versions this has been deprecated and replaced by:

server.servlet.session.persistent=true

Reference https://github.com/spring-projects/spring-boot/issues/2490

Update Tomcat, Jetty and Undertow to serialize session data when the application is stopped and load it again when the application restarts.

Persistent session are opt-in; either by setting persistentSession on the ConfigurableEmbeddedServletContainer or by using the property server.session.persistent=true.

Fixes gh-2490


I solved it by using Redis to persist sessions info.

All you need to do is specify a few options in application.yml file:

server:
  servlet:
    session:
      persistent: true
spring:
  session:
    store-type: redis
  redis:
    host: localhost
    port: 6379
 ...

build.gradle

    plugins {
       id 'java'
       id 'io.spring.dependency-management' version '1.0.6.RELEASE'
       id 'org.springframework.boot' version '2.1.3.RELEASE'
   }
    ...
    // Spring Framework
    compile(
            'org.springframework.boot:spring-boot-starter-web',
            'org.springframework.boot:spring-boot-starter-data-jpa',
            'org.springframework.data:spring-data-redis',
            'org.springframework.boot:spring-boot-starter-security'
    )
    ...

Works perfect with Spring Boot 2.1.3


I just figured this out myself. Everytime the application is started, Spring generates a new random temporary directory in /tmp for Tomcat's base directory (e.g. /tmp/tomcat.5990562997404648887.8080). Since it uses a different folder on each start, Tomcat has no way to restore the session.

This can be worked around by setting your own base directory with server.tomcat.basedir=/tmp. However, I don't consider this a fix since it requires setting an operating system specific directory, so I opened a bug about this: https://github.com/spring-projects/spring-boot/issues/2490