Spring Session

Spring Boot provides Spring Session auto-configuration for a wide range of data stores. When building a servlet web application, the following stores can be auto-configured:spring-doc.cn

The servlet auto-configuration replaces the need to use @Enable*HttpSession.spring-doc.cn

If a single Spring Session module is present on the classpath, Spring Boot uses that store implementation automatically. If you have more than one implementation, Spring Boot uses the following order for choosing a specific implementation:spring-doc.cn

  1. Redisspring-doc.cn

  2. JDBCspring-doc.cn

  3. Hazelcastspring-doc.cn

  4. MongoDBspring-doc.cn

  5. If none of Redis, JDBC, Hazelcast and MongoDB are available, we do not configure a SessionRepository.spring-doc.cn

When building a reactive web application, the following stores can be auto-configured:spring-doc.cn

The reactive auto-configuration replaces the need to use @Enable*WebSession.spring-doc.cn

Similar to the servlet configuration, if you have more than one implementation, Spring Boot uses the following order for choosing a specific implementation:spring-doc.cn

  1. Redisspring-doc.cn

  2. MongoDBspring-doc.cn

  3. If neither Redis nor MongoDB are available, we do not configure a ReactiveSessionRepository.spring-doc.cn

Each store has specific additional settings. For instance, it is possible to customize the name of the table for the JDBC store, as shown in the following example:spring-doc.cn

spring.session.jdbc.table-name=SESSIONS
spring:
  session:
    jdbc:
      table-name: "SESSIONS"

For setting the timeout of the session you can use the spring.session.timeout property. If that property is not set with a servlet web application, the auto-configuration falls back to the value of server.servlet.session.timeout.spring-doc.cn

You can take control over Spring Session’s configuration using @Enable*HttpSession (servlet) or @Enable*WebSession (reactive). This will cause the auto-configuration to back off. Spring Session can then be configured using the annotation’s attributes rather than the previously described configuration properties.spring-doc.cn