Languages
[Edit]
EN

Spring Boot - persist session

9 points
Created by:
Kara
541

In this article, we would like to show how in Spring Boot, store session in the file, preventing losing session when the server is restarted (logging-out user too).

Quick solution (put following line into application.properties file):

server.servlet.session.store-dir=/var/lib/path/to/my-application/sessions

 

Practical example

It is necessary to indicate store directory path in application.properties file:

...

server.servlet.session.persistent=true
server.servlet.session.store-dir=/var/lib/path/to/my-application/sessions

...

How does it work?

  • Always on shutdown, Spring Boot Application will save sessions to a file inside:
    /var/lib/path/to/my-application/sessions directory.
  • Always on startup, Spring Boot Application will load available sessions from a file inside:
    /var/lib/path/to/my-application/sessions directory.

Note: the presented solution was tested under Spring Boot 2.4 that was working with Tomcat 9.

 

See also

  1. Spring Boot - store session ID in MySQL database 

Alternative titles

  1. Spring Boot - store session in file (prevent session loosing on restart)
  2. Spring Boot - prevent session loosing on Tomcat restart
  3. Spring Boot - enable session saving in file on restart
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join