Spring Session - HttpSession (Quick Start)
本指南描述了如何使用 Spring 会话以透明的方式利用 Redis 来支持基于 XML 配置的 Web 应用程序的 HttpSession
。
This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s HttpSession
with XML-based configuration.
您可以在 httpsession-xml sample application 中找到完整指南。 |
You can find the completed guide in the httpsession-xml-sample. |
Updating Dependencies
在使用 Spring 会话之前,您必须更新依赖。如果您使用 Maven,您必须添加以下依赖:
Before you use Spring Session, you must update your dependencies. If you use Maven, you must add the following dependencies:
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>{spring-session-version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>{lettuce-core-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>{spring-core-version}</version>
</dependency>
</dependencies>
Spring XML Configuration
在添加所需的依赖项后,我们可以创建我们的 Spring 配置。Spring 配置负责创建一个 Servlet 过滤器,该过滤器将 HttpSession
实现替换为由 Spring Session 支持的实现。为此,请添加以下 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.
To do so, add the following Spring Configuration:
Unresolved include directive in modules/ROOT/pages/guides/xml-redis.adoc - include::example$spring-session-samples/spring-session-sample-xml-redis/src/main/webapp/WEB-INF/spring/session.xml[]
1 | We use the combination of <context:annotation-config/> and RedisHttpSessionConfiguration because Spring Session does not yet provide XML Namespace support (see gh-104).
This creates a Spring Bean with the name of springSessionRepositoryFilter that 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 Redis. |
2 | We create a RedisConnectionFactory that connects Spring Session to the Redis Server.
We configure the connection to connect to localhost on the default port (6379)
For more information on configuring Spring Data Redis, see the {docs-url}/spring-data/data-redis/docs/{spring-data-redis-version}/reference/html/[reference documentation]. |
XML Servlet Container Initialization
我们的 Spring Configuration 创建了一个名为 springSessionRepositoryFilter
的 Spring Bean,它实现了 Filter
。springSessionRepositoryFilter
Bean 负责使用由 Spring Session 支持的自定义实现来替换 HttpSession
。
Our httpsession-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 can do so with the following configuration:
Unresolved include directive in modules/ROOT/pages/guides/xml-redis.adoc - include::example$spring-session-samples/spring-session-sample-xml-redis/src/main/webapp/WEB-INF/web.xml[]
Unresolved include directive in modules/ROOT/pages/guides/xml-redis.adoc - include::example$spring-session-samples/spring-session-sample-xml-redis/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:
Unresolved include directive in modules/ROOT/pages/guides/xml-redis.adoc - include::example$spring-session-samples/spring-session-sample-xml-redis/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 by the name of springSessionRepositoryFilter
and cast it to a Filter
.
For every request that DelegatingFilterProxy
is invoked, the springSessionRepositoryFilter
is invoked.
httpsession-xml
Sample Application
本节介绍如何使用 httpsession-xml
示例应用程序。
This section describes how to work with the httpsession-xml
sample application.
Running the httpsession-xml
Sample Application
您可以获取 源代码 并调用以下命令运行示例:
You can run the sample by obtaining the source code and invoking the following command:
要让示例发挥作用,你必须在 localhost 上 install Redis 2.8+ 并使用默认端口 (6379) 运行它。或者,你可以更新 |
For the sample to work, you must install Redis 2.8+ on localhost and run it with the default port (6379).
Alternatively, you can update the |
$ ./gradlew :spring-session-sample-xml-redis: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-xml
Sample Application
现在,您可以尝试使用该应用程序了。使用以下信息填写表单:
Now you can try using the application. 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?
我们与 HttpSession
中显示的标准 SessionServlet
进行交互,这在下表中有所说明:
We interact with the standard HttpSession
in the SessionServlet
shown in the following listing:
Unresolved include directive in modules/ROOT/pages/guides/xml-redis.adoc - include::example$spring-session-samples/spring-session-sample-xml-redis/src/main/java/sample/SessionServlet.java[]
我们不使用 Tomcat 的 HttpSession
,而是将值保存在 Redis 中。Spring Session 在浏览器中创建一个名为 SESSION 的 cookie。该 cookie 包含会话 ID。您可以查看 cookie(使用 Chrome 或 Firefox)。
Instead of using Tomcat’s HttpSession
, we persist the values in Redis.
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).
您可以使用 redis-cli 删除该会话。例如,在基于 Linux 的系统中,您可以键入以下内容:
You can remove the session using redis-cli. For example, on a Linux based system you can type the following:
$ redis-cli keys '*' | xargs redis-cli del
Redis 文档包含有关 installing redis-cli 的说明。 |
The Redis documentation has instructions for installing redis-cli. |
或者,您也可以删除显式键。为此,将以下内容输入到您的终端中,并确保使用 SESSION cookie 的值替换 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
:
Alternatively, you can also delete the explicit key. To do so, enter the following into your terminal, being sure to replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
with the value of your SESSION cookie:
$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
现在,您可以访问 [role="bare"][role="bare"]http://localhost:8080/ 中的应用程序并看到我们添加的属性不再显示。
Now you can visit the application at [role="bare"]http://localhost:8080/ and see that the attribute we added is no longer displayed.