Spring Session - HttpSession (Quick Start)

此指南介绍如何使用 Spring Session 透明地利用关系数据库作为基于 XML 的配置 Web 应用程序的 HttpSession

This guide describes how to use Spring Session to transparently leverage a relational to back a web application’s HttpSession with XML based configuration.

您可以在httpsession-jdbc-xml sample application中找到已完成指南。

You can find the completed guide in the httpsession-jdbc-xml-sample.

Updating Dependencies

在你使用 Spring Session 之前,你必须更新你的依赖项。如果你使用的是 Maven,你必须添加以下依赖项:

Before you use Spring Session, you must update your dependencies. If you are using Maven, you must add the following dependencies:

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

	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-jdbc</artifactId>
		<version>{spring-session-version}</version>
		<type>pom</type>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>{spring-core-version}</version>
	</dependency>
</dependencies>

Spring XML Configuration

在添加必需的依赖项后,我们可以创建 Spring 配置。Spring 配置负责创建一个 servlet 筛选器,它使用 Spring Session 支持的实现替换 HttpSession 实现。以下清单展示了如何添加以下 Spring 配置:

After adding the required dependencies, we can create our Spring configuration. The Spring configuration is responsible for creating a servlet filter that replaces the HttpSession implementation with an implementation backed by Spring Session. The following listing shows how to add the following Spring Configuration:

src/main/webapp/WEB-INF/spring/session.xml
Unresolved include directive in modules/ROOT/pages/guides/xml-jdbc.adoc - include::example$spring-session-samples/spring-session-sample-xml-jdbc/src/main/webapp/WEB-INF/spring/session.xml[]
1 We use the combination of <context:annotation-config/> and JdbcHttpSessionConfiguration because Spring Session does not yet provide XML Namespace support (see gh-104). This creates a Spring bean with the name of springSessionRepositoryFilter. That bean implements Filter. The filter is in charge of replacing the HttpSession implementation to be backed by Spring Session. In this instance, Spring Session is backed by a relational database.
2 We create a dataSource that connects Spring Session to an embedded instance of an H2 database. We configure the H2 database to create database tables by using the SQL script that is included in Spring Session.
3 We create a transactionManager that manages transactions for previously configured dataSource.

有关如何配置数据访问相关问题的其他信息,请参阅 {docs-url}/spring/docs/{spring-core-version}/spring-framework-reference/data-access.html[Spring Framework Reference Documentation]。

For additional information on how to configure data access-related concerns, see the {docs-url}/spring/docs/{spring-core-version}/spring-framework-reference/data-access.html[Spring Framework Reference Documentation].

XML Servlet Container Initialization

我们的 Spring Configuration 创建了一个名为 springSessionRepositoryFilter 的 Spring bean,它实现了 FilterspringSessionRepositoryFilter bean 负责使用 Spring Session 支持的自定义实现替换 HttpSession

Our httpsession-jdbc-xml-spring-configuration created a Spring bean named springSessionRepositoryFilter that implements Filter. The springSessionRepositoryFilter bean is responsible for replacing the HttpSession with a custom implementation that is backed by Spring Session.

为了让 Filter 发挥其作用,我们需要指示 Spring 加载我们的 session.xml 配置。我们使用以下配置执行此操作:

In order for our Filter to do its magic, we need to instruct Spring to load our session.xml configuration. We do so with the following configuration:

src/main/webapp/WEB-INF/web.xml
Unresolved include directive in modules/ROOT/pages/guides/xml-jdbc.adoc - include::example$spring-session-samples/spring-session-sample-xml-jdbc/src/main/webapp/WEB-INF/web.xml[]
Unresolved include directive in modules/ROOT/pages/guides/xml-jdbc.adoc - include::example$spring-session-samples/spring-session-sample-xml-jdbc/src/main/webapp/WEB-INF/web.xml[]

{docs-url}/spring/docs/{spring-core-version}/spring-framework-reference/core.html#context-create[ContextLoaderListener] 读入 contextConfigLocation,并拾取到我们的 session.xml 配置。

The {docs-url}/spring/docs/{spring-core-version}/spring-framework-reference/core.html#context-create[ContextLoaderListener] reads the contextConfigLocation and picks up our session.xml configuration.

最后,我们需要确保我们的 Servlet 容器(即 Tomcat)针对每个请求使用我们的 springSessionRepositoryFilter。以下代码段为我们执行最后一步:

Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our springSessionRepositoryFilter for every request. The following snippet performs this last step for us:

src/main/webapp/WEB-INF/web.xml
Unresolved include directive in modules/ROOT/pages/guides/xml-jdbc.adoc - include::example$spring-session-samples/spring-session-sample-xml-jdbc/src/main/webapp/WEB-INF/web.xml[]

{docs-url}/spring-framework/docs/{spring-core-version}/javadoc-api/org/springframework/web/filter/DelegatingFilterProxy.html[DelegatingFilterProxy] 按名称查找一个 Bean springSessionRepositoryFilter,并将其转换为 Filter。对于调用 DelegatingFilterProxy`的每个请求,都调用 `springSessionRepositoryFilter

The {docs-url}/spring-framework/docs/{spring-core-version}/javadoc-api/org/springframework/web/filter/DelegatingFilterProxy.html[DelegatingFilterProxy] looks up a bean named springSessionRepositoryFilter and casts it to a Filter. For every request on which DelegatingFilterProxy is invoked, the springSessionRepositoryFilter is invoked.

httpsession-jdbc-xml Sample Application

本节描述了如何使用 httpsession-jdbc-xml 示例应用程序。

This section describes how to work with the httpsession-jdbc-xml Sample Application.

Running the httpsession-jdbc-xml Sample Application

您可以获取 源代码 并调用以下命令运行示例:

You can run the sample by obtaining the source code and invoking the following command:

$ ./gradlew :spring-session-sample-xml-jdbc:tomcatRun

您现在应该能够访问 [role="bare"][role="bare"]http://localhost:8080/ 中的应用程序。

You should now be able to access the application at [role="bare"]http://localhost:8080/

Exploring the httpsession-jdbc-xml Sample Application

现在你可以尝试使用此应用程序。为此,请填写以下信息的表单:

Now you can try using the application. To do so, fill out the form with the following information:

  • Attribute Name: username

  • Attribute Value: rob

然后,请点击 Set Attribute 按钮。您现在应该可以看到表中显示的值了。

Now click the Set Attribute button. You should now see the values displayed in the table.

How Does It Work?

我们在以下 SessionServlet 中与标准 HttpSession 进行交互:

We interact with the standard HttpSession in the following SessionServlet:

src/main/java/sample/SessionServlet.java
Unresolved include directive in modules/ROOT/pages/guides/xml-jdbc.adoc - include::example$spring-session-samples/spring-session-sample-xml-jdbc/src/main/java/sample/SessionServlet.java[]

不使用 Tomcat 的 HttpSession,我们在 H2 数据库中永久保存这些值。Spring Session 会在你的浏览器中创建一个名为 SESSION 的 cookie。该 cookie 包含你会话的 ID。你可以使用 ChromeFirefox 查看 cookie。

Instead of using Tomcat’s HttpSession, we persist the values in the H2 database. Spring Session creates a cookie named SESSION in your browser. That cookie contains the ID of your session. You can view the cookies (with Chrome or Firefox).

你可以使用位于 [role="bare"][role="bare"]http://localhost:8080/h2-console/ 的 H2 web 控制台来移除会话(为 JDBC URL 使用 jdbc:h2:mem:testdb)。

You can remove the session by using H2 web console available at: [role="bare"]http://localhost:8080/h2-console/ (use jdbc:h2:mem:testdb for JDBC URL)

现在你可以访问位于 [role="bare"][role="bare"]http://localhost:8080/ 的应用,并观察我们添加的属性不再显示。

Now you can visit the application at [role="bare"]http://localhost:8080/ and observe that the attribute we added is no longer displayed.