# 8.5. spring 会话

Spring Boot为各种数据存储提供[Spring Session自动配置。](https://spring.io/projects/spring-session)构建 servlet Web 应用程序时，可以自动配置以下存储：

* Redis
* JDBC
* Hazelcast
* MongoDB

此外，[Spring Boot for Apache Geode](https://github.com/spring-projects/spring-boot-data-geode)提供了[使用 Apache Geode 作为会话存储的自动配置](https://docs.spring.io/spring-boot-data-geode-build/2.0.x/reference/html5/#geode-session)。

Servlet 自动配置取代了使用`@Enable*HttpSession`.

如果类路径上存在单个 Spring Session 模块，Spring Boot 会自动使用该存储实现。如果您有多个实现，Spring Boot 将使用以下顺序来选择特定实现：

1. Redis
2. JDBC
3. Hazelcast
4. MongoDB
5. 如果 Redis、JDBC、Hazelcast 和 MongoDB 都不可用，我们不会配置`SessionRepository`.

构建响应式 Web 应用程序时，可以自动配置以下存储：

* Redis
* MongoDB

反应式自动配置取代了使用`@Enable*WebSession`.

与 servlet 配置类似，如果您有多个实现，Spring Boot 将使用以下顺序来选择特定实现：

1. Redis
2. MongoDB
3. 如果 Redis 和 MongoDB 都不可用，我们不会配置`ReactiveSessionRepository`.

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

```
spring.session.jdbc.table-name=SESSIONS
```

要设置会话超时，您可以使用`spring.session.timeout`属性。如果未使用 servlet Web 应用程序设置`server.servlet.session.timeout`属性，则自动配置将回退到 的值。

您可以使用`@Enable*HttpSession`(servlet) 或`@Enable*WebSession`(reactive)来控制 Spring Session 的配置。这将导致自动配置停止。然后可以使用注释的属性而不是前面描述的配置属性来配置 Spring Session。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/8.-wang-luo/8.5.-spring-hui-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
