对于最新的稳定版本,请使用 Spring Session 3.4.0! |
Spring Session - Spring Boot
本指南描述了如何使用 Spring Session 透明地利用关系数据库来支持 Web 应用程序的HttpSession
当您使用 Spring Boot 时。
您可以在 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 模块提供了依赖项 Management,因此您无需显式声明依赖项版本。
Spring Boot 配置
添加所需的依赖项后,我们可以创建 Spring Boot 配置。
得益于一流的自动配置支持,设置由关系数据库支持的 Spring Session 就像将单个配置属性添加到application.properties
.
下面的清单显示了如何做到这一点:
spring.session.store-type=jdbc # Session store type.
如果 Classpath 上存在单个 Spring Session 模块,则 Spring Boot 会自动使用该 store 实现。 如果您有多个实施,则必须选择要用于存储会话的 StoreType,如上所示。
在后台,Spring Boot 应用等效于手动添加@EnableJdbcHttpSession
注解。
这将创建一个名称为springSessionRepositoryFilter
.该 bean 实现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 文档的 Configure a DataSource 部分。
Servlet 容器初始化
我们的 Spring Boot 配置创建了一个名为springSessionRepositoryFilter
实现Filter
.
这springSessionRepositoryFilter
bean 负责将HttpSession
使用由 Spring Session 支持的自定义实现。
为了我们的Filter
为了发挥它的魔力,Spring 需要加载我们的Config
类。
最后,我们需要确保我们的 Servlet 容器(即 Tomcat)使用我们的springSessionRepositoryFilter
对于每个请求。
幸运的是,Spring Boot 为我们处理了这两个步骤。
httpsession-jdbc-boot
示例应用程序
httpsession-jdbc-boot 示例应用程序演示了如何使用 Spring Session 透明地利用 H2 数据库来支持 Web 应用程序的HttpSession
当您使用 Spring Boot 时。
运行httpsession-jdbc-boot
示例应用程序
您可以通过获取源码并调用以下命令来运行该示例:
$ ./gradlew :spring-session-sample-boot-jdbc:bootRun
您现在应该能够访问 localhost:8080/ 中的应用程序
探索安全性示例应用程序
您现在可以尝试使用该应用程序。 为此,请输入以下内容以登录:
-
用户名 user
-
密码 密码
现在单击 Login 按钮。
您现在应该会看到一条消息,指示您已使用之前输入的用户登录。
用户的信息存储在 H2 数据库中,而不是 Tomcat 的数据库中HttpSession
实现。
它是如何工作的?
而不是使用 Tomcat 的HttpSession
,我们将值保留在 H2 数据库中。
Spring Session 取代了HttpSession
使用由关系数据库支持的实现。
当 Spring Security 的SecurityContextPersistenceFilter
保存SecurityContext
到HttpSession
,然后将其保存到 H2 数据库中。
当新的HttpSession
时,Spring Session 会创建一个名为SESSION
在浏览器中。该 Cookie 包含会话的 ID。
您可以查看 Cookie(使用 Chrome 或 Firefox)。
您可以使用以下网址提供的 H2 Web 控制台删除会话:localhost:8080/h2-console/(使用jdbc:h2:mem:testdb
(对于 JDBC URL)。
现在,您可以在 localhost:8080/ 上访问应用程序,并看到我们不再经过身份验证。