# Spring Session- Spring 启动
本指南描述了在使用 Spring 引导时如何使用 Spring Session 来透明地利用关系数据库来支持 Web 应用程序的HttpSession
。
你可以在httpsession-jdbc-boot 示例应用程序中找到完整的指南。 |
---|
# 更新依赖项
在使用 Spring Session 之前,你必须更新你的依赖关系。我们假设你正在使用一个有效的启动 Web 应用程序。如果使用 Maven,则必须添加以下依赖项:
POM.xml
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
</dependencies>
Spring 启动为 Spring Session 模块提供了依赖管理,因此不需要显式地声明依赖版本。
# Spring 引导配置
在添加了所需的依赖项之后,我们就可以创建我们的 Spring 启动配置了。多亏了一流的自动配置支持,在关系数据库支持下设置 Spring Session 就像在application.properties
中添加一个配置属性一样简单。下面的清单展示了如何做到这一点:
SRC/主/资源/应用程序.properties
spring.session.store-type=jdbc # Session store type.
如果在 Classpath 上存在单个 Spring Session 模块,则 Spring 引导将自动使用该存储实现。如果有多个实现,则必须选择要用来存储会话的 StoreType,如上面所示。
在这种情况下, Spring boot 应用的配置相当于手动添加@EnableJdbcHttpSession
注释。这将创建一个名为springSessionRepositoryFilter
的 Spring Bean。 Bean 实现Filter
。过滤器负责替换要由 Spring Session 支持的HttpSession
实现。
你可以使用application.properties
来进一步自定义。下面的清单展示了如何做到这一点:
SRC/主/资源/应用程序.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/[email protected]@[email protected]@.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 引导文档的Spring Session (opens new window)部分。
# 配置DataSource
Spring 引导会自动创建一个DataSource
,它将 Spring Session 连接到 H2 数据库的嵌入式实例。在生产环境中,你需要更新配置以指向关系数据库。例如,你可以在应用程序中包含以下内容:
SRC/主/资源/应用程序.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 引导文档的配置数据源 (opens new window)部分。
# Servlet 容器初始化
我们的Spring Boot Configuration创建了一个名为springSessionRepositoryFilter
的 Spring Bean,它实现了Filter
。springSessionRepositoryFilter
Bean 负责用 Spring Session 支持的自定义实现替换HttpSession
。
为了让我们的Filter
发挥其魔力, Spring 需要加载我们的Config
类。最后,我们需要确保我们的 Servlet 容器(即 Tomcat)为每个请求使用我们的springSessionRepositoryFilter
。幸运的是,Boot 为我们解决了这两个步骤。
# httpsession-jdbc-boot
示例应用程序
HttpSession-JDBC-Boot 示例应用程序演示了如何在使用 Spring 引导时使用 Spring Session 透明地利用 H2 数据库来支持 Web 应用程序的HttpSession
。
# 运行httpsession-jdbc-boot
示例应用程序
你可以通过获取源代码 (opens new window)并调用以下命令来运行示例:
$ ./gradlew :spring-session-sample-boot-jdbc:bootRun
现在你应该可以在http://localhost:8080/ (opens new window)上访问应用程序了。
# 探索安全示例应用程序
你现在可以尝试使用该应用程序了。要这样做,请输入以下内容以进行登录:
用户 Name User
密码 密码
现在点击登录按钮。你现在应该会看到一条消息,该消息指示你是用先前输入的用户登录的。用户的信息存储在 H2 数据库中,而不是 Tomcat 的HttpSession
实现。
# 它是如何工作的?
我们不使用 Tomcat 的HttpSession
,而是将这些值保存在 H2 数据库中。 Spring Session 使用由关系数据库支持的实现来替换HttpSession
。当 Spring Security 的SecurityContextPersistenceFilter
将SecurityContext
保存到HttpSession
时,它将被持久化到 H2 数据库中。
当创建一个新的HttpSession
时, Spring Session 将在浏览器中创建一个名为SESSION
的 cookie。该 cookie 包含你的会话的 ID。你可以查看 cookies(使用Chrome (opens new window)或Firefox (opens new window))。
你可以通过使用 H2Web 控制台来删除会话:http://localhost:8080/h2-console/ (opens new window)(对于 JDBC URL 使用jdbc:h2:mem:testdb
)。
现在,你可以访问http://localhost:8080/ (opens new window)上的应用程序,并看到我们不再经过身份验证。