此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.4.0spring-doc.cadn.net.cn

Spring Session - Spring Boot

本指南描述了如何使用 Spring Session 透明地利用关系数据库来支持 Web 应用程序的HttpSession当您使用 Spring Boot 时。spring-doc.cadn.net.cn

您可以在 httpsession-jdbc-boot 示例应用程序中找到完整的指南。

更新依赖项

在使用 Spring Session 之前,您必须更新依赖项。 我们假设您正在使用一个正在运行的 Spring Boot Web 应用程序。 如果您使用 Maven,则必须添加以下依赖项:spring-doc.cadn.net.cn

pom.xml
<dependencies>
	<!-- ... -->

	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-jdbc</artifactId>
	</dependency>
</dependencies>

Spring Boot 为 Spring Session 模块提供了依赖项 Management,因此您无需显式声明依赖项版本。spring-doc.cadn.net.cn

Spring Boot 配置

添加所需的依赖项后,我们可以创建 Spring Boot 配置。 多亏了一流的自动配置支持,只需添加依赖项 Spring Boot 就可以为我们设置由关系数据库支持的 Spring Session。spring-doc.cadn.net.cn

如果 Classpath 上存在单个 Spring Session 模块,则 Spring Boot 会自动使用该 store 实现。 如果您有多个实施,则必须选择要用于存储会话的 StoreType,如上所示。spring-doc.cadn.net.cn

在后台,Spring Boot 应用等效于手动添加@EnableJdbcHttpSession注解。 这将创建一个名称为springSessionRepositoryFilter.该 bean 实现Filter. 过滤器负责替换HttpSession实现,由 Spring Session 支持。spring-doc.cadn.net.cn

您可以使用application.properties. 下面的清单显示了如何做到这一点:spring-doc.cadn.net.cn

src/main/resources/application.properties
server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds are used.
spring.session.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.table-name=SPRING_SESSION # Name of the database table used to store sessions.

有关更多信息,请参阅 Spring Boot 文档的 Spring Session 部分。spring-doc.cadn.net.cn

配置DataSource

Spring Boot 会自动创建一个DataSource将 Spring Session 连接到 H2 数据库的嵌入式实例。 在生产环境中,您需要更新配置以指向关系数据库。 例如,您可以在 application.properties 中包含以下内容:spring-doc.cadn.net.cn

src/main/resources/application.properties
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
spring.datasource.password= # Login password of the database.

有关更多信息,请参阅 Spring Boot 文档的 Configure a DataSource 部分。spring-doc.cadn.net.cn

Servlet 容器初始化

我们的 Spring Boot 配置创建了一个名为springSessionRepositoryFilter实现Filter. 这springSessionRepositoryFilterbean 负责替换HttpSession使用由 Spring Session 支持的自定义实现。spring-doc.cadn.net.cn

为了我们的Filter为了发挥它的魔力,Spring 需要加载我们的Config类。 最后,我们需要确保我们的 Servlet 容器(即 Tomcat)使用我们的springSessionRepositoryFilter对于每个请求。 幸运的是,Spring Boot 为我们处理了这两个步骤。spring-doc.cadn.net.cn

httpsession-jdbc-boot示例应用程序

httpsession-jdbc-boot 示例应用程序演示了如何使用 Spring Session 透明地利用 H2 数据库来支持 Web 应用程序的HttpSession当您使用 Spring Boot 时。spring-doc.cadn.net.cn

运行httpsession-jdbc-boot示例应用程序

您可以通过获取源码并调用以下命令来运行该示例:spring-doc.cadn.net.cn

$ ./gradlew :spring-session-sample-boot-jdbc:bootRun

您现在应该能够访问 localhost:8080/ 中的应用程序spring-doc.cadn.net.cn

探索安全性示例应用程序

您现在可以尝试使用该应用程序。 为此,请输入以下内容以登录:spring-doc.cadn.net.cn

现在单击 Login 按钮。 您现在应该会看到一条消息,指示您已使用之前输入的用户登录。 用户的信息存储在 H2 数据库中,而不是 Tomcat 的数据库中HttpSession实现。spring-doc.cadn.net.cn

它是如何工作的?

而不是使用 Tomcat 的HttpSession,我们将值保留在 H2 数据库中。 Spring Session 取代了HttpSession使用由关系数据库支持的实现。 当 Spring Security 的SecurityContextPersistenceFilter保存SecurityContextHttpSession,然后将其保存到 H2 数据库中。spring-doc.cadn.net.cn

当新的HttpSession时,Spring Session 会创建一个名为SESSION在浏览器中。该 Cookie 包含会话的 ID。 您可以查看 Cookie(使用 ChromeFirefox)。spring-doc.cadn.net.cn

您可以使用以下网址提供的 H2 Web 控制台删除会话:localhost:8080/h2-console/(使用jdbc:h2:mem:testdb(对于 JDBC URL)。spring-doc.cadn.net.cn

现在,您可以在 localhost:8080/ 上访问应用程序,并看到我们不再经过身份验证。spring-doc.cadn.net.cn