简体   繁体   中英

Enable session persistence with Spring Boot and embedded Tomcat

I'm developing an application with Spring Boot and Spring Security by starting the Application class in Eclipse with an embedded Tomcat. Every time I restart the server, my session disappears and I have to log in again which becomes quite annoying.

Is it possible to persist the sessions between server restarts?

I saw this other question on Stackoverflow where someone asks the opposite, which makes me think that this should actually work out-of-the-box:

How to disable Tomact session persistence in Spring Boot via Manager pathname?

I'm running Spring Boot 1.2.1 with Gradle.

btw, I know about Spring Loaded, but sometimes a server restart is unavoidable.

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 just figured this out myself. Everytime the application is started, Spring generates a new random temporary directory in /tmp for Tomcat's base directory (eg /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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM