Spring Session With Redis - HttpSession (Quick Start)
原文地址:https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-redis.html
目录
1. Updating Dependencies(更新依赖关系)
2. Spring Java Configuration(Spring Java配置)
3. Java Servlet Container Initialization(Java Servlet容器初始化)
4. httpsession Sample Application(httpsession示例应用程序)
4.1. Running the httpsession
Sample Application(运行httpsession示例应用程序)
4.2. Exploring the httpsession
Sample Application(探索httpsession示例应用程序)
4.3. How Does It Work?
This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s HttpSession
with Java Configuration.
本指南描述了如何使用Spring Session透明地利用Redis来支持带有Java配置的web应用程序的HttpSession。
You can find the completed guide in the httpsession sample application.
您可以在httpsession示例应用程序中找到完整的指南。
1. Updating Dependencies(更新依赖关系)
Before you use Spring Session, you must update your dependencies. If you are using Maven, you must add the following dependencies:
在使用Spring Session之前,必须更新依赖项。
如果你使用Maven,你必须添加以下依赖项:
pom.xml
<dependencies> <!-- ... --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.4.RELEASE</version> </dependency> </dependencies>
2. Spring Java Configuration(Spring Java配置)
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:
在添加了所需的依赖项之后,我们可以创建Spring配置。
Spring配置负责创建servlet过滤器,该过滤器将HttpSession实现替换为Spring Session所支持的实现。为此,添加以下Spring配置:
@EnableRedisHttpSession public class Config { @Bean public LettuceConnectionFactory connectionFactory() { return new LettuceConnectionFactory(); } }
The
@EnableRedisHttpSession
annotation creates a Spring Bean with the name ofspringSessionRepositoryFilter
that implementsFilter
. The filter is in charge of replacing theHttpSession
implementation to be backed by Spring Session. In this instance, Spring Session is backed by Redis.@EnableRedisHttpSession注释创建了一个名为springSessionRepositoryFilter的Spring Bean,该Bean实现了Filter。过滤器负责将HttpSession实现替换为由Spring Session支持。在本例中,Spring会话由Redis支持。
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 reference documentation.我们创建一个RedisConnectionFactory,将Spring会话连接到Redis服务器。我们将连接配置为在默认端口(6379)上连接到本地主机。有关配置Spring Data Redis的更多信息,请参阅参考文档。
3. Java Servlet Container Initialization(Java Servlet容器初始化)
Our 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.
我们的Spring配置创建了一个名为springSessionRepositoryFilter的Spring Bean,它实现了Filter。
springSessionRepositoryFilter bean负责将HttpSession替换为由Spring Session会话支持的自定义实现。
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:
为了让过滤器发挥作用,Spring需要加载配置类。最后,我们需要确保Servlet容器(即Tomcat)对每个请求都使用springSessionRepositoryFilter。幸运的是,Spring Session提供了一个名为AbstractHttpSessionApplicationInitializer的实用工具类,可以简化这两个步骤。以下是一个例子:
src/main/java/sample/Initializer.java
public class Initializer extends AbstractHttpSessionApplicationInitializer { public Initializer() { super(Config.class); } }
The name of our class (
Initializer
) does not matter. What is important is that we extendAbstractHttpSessionApplicationInitializer
.类的名称(初始化器)并不重要。重要的是我们扩展了AbstractHttpSessionApplicationInitializer。
The first step is to extend
AbstractHttpSessionApplicationInitializer
. Doing so ensures that the Spring Bean by the name ofspringSessionRepositoryFilter
is registered with our Servlet Container for every request.第一步是扩展AbstractHttpSessionApplicationInitializer。这样做可以确保为每个请求向Servlet容器注册名为springSessionRepositoryFilter的Spring Bean。
AbstractHttpSessionApplicationInitializer
also provides a mechanism to ensure Spring loads ourConfig.
AbstractHttpSessionApplicationInitializer还提供了一种机制来确保Spring加载我们的配置。
4. httpsession Sample Application(httpsession示例应用程序)
4.1. Running the httpsession
Sample Application(运行httpsession示例应用程序)
You can run the sample by obtaining the source code and invoking the following command:
您可以通过获得源代码并调用以下命令来运行示例:
$ ./gradlew :spring-session-sample-javaconfig-redis:tomcatRun
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 RedisConnectionFactory
to point to a Redis server. Another option is to use Docker to run Redis on localhost. See Docker Redis repository for detailed instructions.
要使样例工作,您必须在本地主机上安装Redis 2.8+,并使用默认端口(6379)运行它。或者,您可以更新RedisConnectionFactory以指向一个Redis服务器。另一个选项是使用Docker在本地主机上运行Redis。详细说明请参阅Docker Redis知识库。
You should now be able to access the application at http://localhost:8080/
现在,您应该能够通过http://localhost:8080/访问该应用程序
4.2. Exploring the httpsession
Sample Application(探索httpsession示例应用程序)
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
Now click the Set Attribute button. You should now see the values displayed in the table.
现在单击Set属性按钮。现在应该可以看到表中显示的值。
4.3. How Does It Work?
We interact with the standard HttpSession
in the SessionServlet
shown in the following listing:
我们与SessionServlet中的标准HttpSession进行交互,如下面的清单所示:
src/main/java/sample/SessionServlet.java
@WebServlet("/session") public class SessionServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { String attributeName = req.getParameter("attributeName"); String attributeValue = req.getParameter("attributeValue"); req.getSession().setAttribute(attributeName, attributeValue); resp.sendRedirect(req.getContextPath() + "/"); } private static final long serialVersionUID = 2878267318695777395L; }
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).
我们没有使用Tomcat的HttpSession,而是将这些值保存在Redis中。Spring Session在浏览器中创建一个名为Session的cookie。该cookie包含会话的ID。您可以查看cookies(使用Chrome或Firefox)。
You can remove the session by using redis-cli. For example, on a Linux based system you can type the following:
您可以使用redis-cli删除会话。例如,在基于Linux的系统上,您可以输入以下内容:
$ redis-cli keys '*' | xargs redis-cli del
The Redis documentation has instructions for installing redis-cli.
Redis文档中有安装Redis -cli的说明。
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:
或者,您也可以删除显式密钥。
在您的终端中输入以下内容,请确保使用您的会话cookie的值替换7e8383a4-082c-4ffe-a4bc-c40fd3363c5e:
$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
Now you can visit the application at http://localhost:8080/ and see that the attribute we added is no longer displayed.
现在,您可以通过http://localhost:8080/访问该应用程序,并看到我们添加的属性不再显示。
Version 2.2.2.RELEASE
Last updated 2020-03-04 21:20:38 +00:00