Spring Session - Spring Boot
本指南介绍了如何在使用 Spring Boot 时,利用 Spring Session 透明地使用关系型数据库作为 Web 应用 HttpSession
的后端存储。
您可以在 httpsession-jdbc-boot 示例应用中找到完整的指南。 |
更新依赖
在使用 Spring Session 之前,您必须更新您的依赖。我们假设您正在使用一个可工作的 Spring Boot Web 应用。如果您使用 Maven,则必须添加以下依赖:
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
</dependencies>
Spring Boot 为 Spring Session 模块提供了依赖管理,因此您无需显式声明依赖版本。
Spring Boot 配置
添加所需的依赖后,我们可以创建 Spring Boot 配置。得益于一流的自动配置支持,只需添加依赖,Spring Boot 就会为我们设置由关系型数据库支持的 Spring Session。
如果 classpath 中存在单个 Spring Session 模块,Spring Boot 会自动使用该存储实现。如果您有多个实现,则必须选择您希望用于存储 Session 的 StoreType,如上所示。
底层,Spring Boot 应用的配置等同于手动添加 @EnableJdbcHttpSession
注解。这会创建一个名为 springSessionRepositoryFilter
的 Spring Bean。该 Bean 实现了 Filter
。该 Filter 负责将 HttpSession
实现替换为由 Spring Session 支持的实现。
您可以使用 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 的部分。
配置 DataSource
Spring Boot 会自动创建一个 DataSource
,用于将 Spring Session 连接到嵌入式 H2 数据库实例。在生产环境中,您需要更新配置,使其指向您的关系型数据库。例如,您可以在 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 文档中关于配置 DataSource 的部分。
Servlet 容器初始化
我们的Spring Boot 配置创建了一个名为 springSessionRepositoryFilter
的 Spring Bean,该 Bean 实现了 Filter
。springSessionRepositoryFilter
Bean 负责将 HttpSession
替换为由 Spring Session 支持的自定义实现。
为了让我们的 Filter
发挥作用,Spring 需要加载我们的 Config
类。最后,我们需要确保我们的 Servlet 容器(即 Tomcat)为每个请求使用我们的 springSessionRepositoryFilter
。幸运的是,Spring Boot 为我们处理了这两个步骤。
httpsession-jdbc-boot
示例应用
httpsession-jdbc-boot 示例应用展示了如何在您使用 Spring Boot 时,利用 Spring Session 透明地使用 H2 数据库作为 Web 应用 HttpSession
的后端存储。
运行 httpsession-jdbc-boot
示例应用
您可以通过获取源代码并调用以下命令来运行此示例:
$ ./gradlew :spring-session-sample-boot-jdbc:bootRun
您现在应该可以通过 localhost:8080/ 访问该应用。
探索安全示例应用
您现在可以尝试使用该应用。为此,请输入以下内容登录:
-
用户名 user
-
密码 password
现在点击登录按钮。您应该会看到一条消息,指示您已使用之前输入的用户名登录。用户信息存储在 H2 数据库中,而不是 Tomcat 的 HttpSession
实现中。
它是如何工作的?
我们不使用 Tomcat 的 HttpSession
,而是将值持久化到 H2 数据库中。Spring Session 用一个由关系型数据库支持的实现替换了 HttpSession
。当 Spring Security 的 SecurityContextPersistenceFilter
将 SecurityContext
保存到 HttpSession
时,它随后被持久化到 H2 数据库中。
当创建一个新的 HttpSession
时,Spring Session 会在您的浏览器中创建一个名为 SESSION
的 cookie。该 cookie 包含您的 Session ID。您可以查看 cookie(使用 Chrome 或 Firefox)。
您可以通过使用 H2 Web 控制台来删除 Session,该控制台位于:localhost:8080/h2-console/(JDBC URL 请使用 jdbc:h2:mem:testdb
)。
现在您可以访问 localhost:8080/ 上的应用,您会看到我们不再处于认证状态。