Spring Session

Spring Boot 为广泛的数据存储提供了 {url-spring-session-site}[Spring Session] 自动配置。在构建 servlet web 应用程序时,可以自动配置以下存储:

Spring Boot provides {url-spring-session-site}[Spring Session] auto-configuration for a wide range of data stores. When building a servlet web application, the following stores can be auto-configured:

  • Redis

  • JDBC

  • Hazelcast

  • MongoDB

此外,{url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] 提供 {url-spring-boot-for-apache-geode-docs}#geode-session[Apache Geode 用于作为会话存储的自动配置]。

Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-session[auto-configuration for using Apache Geode as a session store].

servlet 自动配置代替了使用 @Enable*HttpSession 的需求。

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

如果类路径中存在一个 Spring Session 模块,Spring Boot 会自动使用该存储实现。如果你有多个实现,Spring Boot 使用以下顺序选择一个特定实现:

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:

  1. Redis

  2. JDBC

  3. Hazelcast

  4. MongoDB

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

在构建反应式网络应用程序时,可以自动配置以下存储:

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

  • Redis

  • MongoDB

响应式自动配置取代了使用 @Enable*WebSession 的需求。

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

与 servlet 配置类似,如果您有多个实现,Spring Boot 会使用以下顺序选择特定实现:

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

  1. Redis

  2. MongoDB

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

每个存储都有特定的附加设置。例如,可以自定义 JDBC 存储的表名,如下例所示:

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:
  session:
    jdbc:
      table-name: "SESSIONS"

要设置会话的超时,可以使用 configprop:spring.session.timeout[] 属性。如果 servlet web 应用程序未使用此属性,则自动配置将回退到 configprop:server.servlet.session.timeout[] 的值。

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

可以使用 @Enable*HttpSession (servlet)或 @Enable*WebSession (响应式)控制 Spring Session 的配置。这将导致自动配置回退。然后可以使用注释的属性(而不是先前描述的配置属性)来配置 Spring Session。

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.