Search code examples
javaspringspring-mvctomcatspring-boot

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.


Solution

  • 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