Spring Session - HttpSession (Quick Start)
本指南描述了如何使用 Spring Session 透明地利用 Redis 为 Web 应用程序的`HttpSession`提供支持,采用的是 Java 配置。
This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s HttpSession
with Java Configuration.
您可以在httpsession sample application中找到已完成指南。 |
You can find the completed guide in the httpsession-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:
<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>
由于我们使用的是 SNAPSHOT 版本,因此我们需要确保添加 Spring Snapshot Maven 存储库。在你的 pom.xml 中必须具有以下内容:
Since we are using a SNAPSHOT version, we need to ensure to add the Spring Snapshot Maven Repository. You must have the following in your pom.xml:
<repositories>
<!-- ... -->
<repository>
<id>spring-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
Spring Java 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/java-redis.adoc - include::example$spring-session-samples/spring-session-sample-javaconfig-redis/src/main/java/sample/Config.java[]
1 | The @EnableRedisHttpSession annotation 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]. |
Java Servlet Container Initialization
我们的 Spring Configuration 创建了一个名为 springSessionRepositoryFilter
的 Spring Bean,它实现了 Filter
。springSessionRepositoryFilter
Bean 负责使用由 Spring Session 支持的自定义实现来替换 HttpSession
。
Our httpsession-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 需要加载我们的 Config
类。最后,我们需要确保我们的 Servlet 容器(即 Tomcat)对每个请求使用我们的 springSessionRepositoryFilter
。幸运的是,Spring Session 提供了一个名为 AbstractHttpSessionApplicationInitializer
的实用程序类,可以轻松完成这两个步骤。以下显示了一个示例:
In order for our Filter
to do its magic, Spring needs to load our Config
class.
Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our springSessionRepositoryFilter
for every request.
Fortunately, Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer
to make both of these steps easy.
The following shows an example:
Unresolved include directive in modules/ROOT/pages/guides/java-redis.adoc - include::example$spring-session-samples/spring-session-sample-javaconfig-redis/src/main/java/sample/Initializer.java[]
我们的类 (Initializer
) 的名称并不重要,重要的是我们必须延伸`AbstractHttpSessionApplicationInitializer`。
The name of our class (Initializer
) does not matter. What is important is that we extend AbstractHttpSessionApplicationInitializer
.
1 | The first step is to extend AbstractHttpSessionApplicationInitializer .
Doing so ensures that the Spring Bean by the name of springSessionRepositoryFilter is registered with our Servlet Container for every request. |
2 | AbstractHttpSessionApplicationInitializer also provides a mechanism to ensure Spring loads our Config . |
httpsession Sample Application
Running the httpsession
Sample Application
您可以获取 源代码 并调用以下命令运行示例:
You can run the sample by obtaining the source code and invoking the following command:
$ ./gradlew :spring-session-sample-javaconfig-redis:tomcatRun
要让示例发挥作用,你必须在 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 |
您现在应该能够访问 [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
Sample Application
现在,你可以尝试使用该应用程序。为此,请使用以下信息填写表单:
Now you can try to use 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?
我们与 HttpSession
中显示的标准 SessionServlet
进行交互,这在下表中有所说明:
We interact with the standard HttpSession
in the SessionServlet
shown in the following listing:
Unresolved include directive in modules/ROOT/pages/guides/java-redis.adoc - include::example$spring-session-samples/spring-session-sample-javaconfig-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 by 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. 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.