最新稳定版请使用Spring Session 3.5.3spring-doc.cadn.net.cn

春季课程 - 春季启动

本指南介绍了如何使用 Spring Session 透明地利用关系数据库来支持网页应用HttpSession当你使用Spring Boot时。spring-doc.cadn.net.cn

你可以在 httpsession-jdbc-boot 示例应用中找到完成的指南。

更新依赖关系

在使用春季会话之前,您必须更新依赖。 我们假设你使用的是一个正常运行的 Spring Boot 网页应用。 如果你使用 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 模块提供依赖管理,因此你无需明确声明依赖版本。spring-doc.cadn.net.cn

Spring Boot配置

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

如果类路径上存在单个 Spring Session 模块,Spring Boot 会自动使用该存储实现。 如果你有多个实现,必须选择你希望用来存储会话的StoreType,如上文所示。spring-doc.cadn.net.cn

在底层,Spring Boot 应用的配置相当于手动添加@EnableJdbcHttpSession注解。 这会形成一种名为springSessionRepositoryFilter.豆子的实现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-doc.cadn.net.cn

配置数据来源

Spring Boot会自动生成数据来源它将 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 文档中的“配置数据源”部分。spring-doc.cadn.net.cn

Servlet 容器初始化

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

为了我们的Filter要施展它的魔力,Spring需要加载我们的配置类。 最后,我们需要确保我们的Servlet容器(即Tomcat)使用我们的springSessionRepositoryFilter每一个请求。 幸运的是,Spring Boot 帮我们解决了这两个步骤。spring-doc.cadn.net.cn

HTTPsession-jdbc-boot示例应用

httpsession-jdbc-boot 示例应用演示了如何利用 Spring Session 透明地利用 H2 数据库来支持网页应用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

现在点击登录按钮。 你现在应该会看到一条消息,提示你已经登录了之前输入的用户。 用户信息存储在H2数据库中,而非Tomcat的数据库HttpSession实现。spring-doc.cadn.net.cn

它是如何运作的?

而不是用Tomcat的HttpSession,我们将这些值持久化在H2数据库中。 Spring Session取代了HttpSession实现时,它得到了关系型数据库的支持。 当春季安全SecurityContextPersistenceFilter节省安全上下文前往HttpSession然后,它会被持久化到H2数据库中。spring-doc.cadn.net.cn

当新的HttpSession创建了 Spring Session 创建一个名为会期在你的浏览器里。这个Cookie里包含了你会话的ID。 你可以查看这些Cookie(使用ChromeFirefox)。spring-doc.cadn.net.cn

你可以使用H2网页控制台(localhost:8080/h2-console/,使用)删除会话。JDBC:H2:mem:testDB用于JDBC网址)。spring-doc.cadn.net.cn

现在你可以访问 localhost:8080/ 的应用程序,看到我们不再认证。spring-doc.cadn.net.cn