spring-session+redis解决session共存问题
2017-06-14 15:14
639 查看
原文地址:http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession.html
Rob WinchVersion 1.3.1.RELEASE
Table of Contents
Updating
Dependencies
Spring
Java Configuration
Java
Servlet Container Initialization
httpsession
Sample Application
Running
the httpsession Sample Application
Exploring
the httpsession Sample Application
How
does it work?
This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s
Java Configuration.
Before you use Spring Session, you must ensure to update your dependencies. If you are using Maven, ensure to add the following dependencies:
pom.xml
After adding the required dependencies, we can create our Spring configuration. The Spring configuration is responsible for creating a Servlet Filter that replaces the
with an implementation backed by Spring Session. Add the following Spring Configuration:
Our Spring Configuration created
a Spring Bean named
implements
is responsible for replacing the
a custom implementation that is backed by Spring Session.
In order for our
Spring needs to load our
we need to ensure that our Servlet Container (i.e. Tomcat) uses our
every request. Fortunately, Spring Session provides a utility class named
of these steps extremely easy. You can find an example below:
src/main/java/sample/Initializer.java
You can run the sample by obtaining the source code and invoking the following
command:
You should now be able to access the application at http://localhost:8080/
Try using the application. 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.
We interact with the standard
the
src/main/java/sample/SessionServlet.java
Instead of using Tomcat’s
are actually persisting the values in Redis. Spring Session creates a cookie named SESSION in your browser that contains the id of your session. Go ahead and view the cookies (click for help with Chrome or Firefox).
If you like, you can easily remove the session using redis-cli. For example, on a Linux based system you can type:
Alternatively, you can also delete the explicit key. Enter the following into your terminal ensuring to replace
the value of your SESSION cookie:
Now visit the application at http://localhost:8080/ and observe that the attribute we added is no longer displayed.
Version 1.3.1.RELEASE
Last updated 2017-04-27 18:42:57 +00:00
Spring Session - HttpSession (Quick Start)
Rob WinchVersion 1.3.1.RELEASETable of Contents
Updating
Dependencies
Spring
Java Configuration
Java
Servlet Container Initialization
httpsession
Sample Application
Running
the httpsession Sample Application
Exploring
the httpsession Sample Application
How
does it work?
This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s
HttpSessionwith
Java Configuration.
The completed guide can be found in the httpsession sample application. |
Updating
Dependencies
Before you use Spring Session, you must ensure to update your dependencies. If you are using Maven, ensure to add the following dependencies:pom.xml
<dependencies> <!-- ... --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.3.1.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>biz.paluch.redis</groupId> <artifactId>lettuce</artifactId> <version>3.5.0.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.4.RELEASE</version> </dependency> </dependencies>
Spring
Java Configuration
After adding the required dependencies, we can create our Spring configuration. The Spring configuration is responsible for creating a Servlet Filter that replaces the HttpSessionimplementation
with an implementation backed by Spring Session. Add the following Spring Configuration:
@EnableRedisHttpSession public class Config { @Bean public LettuceConnectionFactory connectionFactory() { return new LettuceConnectionFactory(); } }
The @EnableRedisHttpSessionannotation creates a Spring Bean with the name of springSessionRepositoryFilterthat implements Filter. The filter is what is in charge of replacing the HttpSessionimplementation to be backed by Spring Session. In this instance Spring Session is backed by Redis. | |
We create a RedisConnectionFactorythat 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, refer to the reference documentation. |
Java
Servlet Container Initialization
Our Spring Configuration createda Spring Bean named
springSessionRepositoryFilterthat
implements
Filter. The
springSessionRepositoryFilterbean
is responsible for replacing the
HttpSessionwith
a custom implementation that is backed by Spring Session.
In order for our
Filterto do its magic,
Spring needs to load our
Configclass. Last
we need to ensure that our Servlet Container (i.e. Tomcat) uses our
springSessionRepositoryFilterfor
every request. Fortunately, Spring Session provides a utility class named
AbstractHttpSessionApplicationInitializerboth
of these steps extremely easy. You can find an example below:
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 extend AbstractHttpSessionApplicationInitializer. |
The first step is to extend AbstractHttpSessionApplicationInitializer. This ensures that the Spring Bean by the name springSessionRepositoryFilteris registered with our Servlet Container for every request. | |
AbstractHttpSessionApplicationInitializeralso provides a mechanism to easily 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 followingcommand:
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 LettuceConnectionFactoryto point to a Redis server. |
$ ./gradlew :samples:httpsession:tomcatRun
You should now be able to access the application at http://localhost:8080/
Exploring
the httpsession Sample Application
Try using the application. 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.
How
does it work?
We interact with the standard HttpSessionin
the
SessionServletshown below:
src/main/java/sample/SessionServlet.java
@WebServlet("/session") public class SessionServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 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
are actually persisting the values in Redis. Spring Session creates a cookie named SESSION in your browser that contains the id of your session. Go ahead and view the cookies (click for help with Chrome or Firefox).
If you like, you can easily remove the session using redis-cli. For example, on a Linux based system you can type:
$ redis-cli keys '*' | xargs redis-cli del
The Redis documentation has instructions for installing redis-cli. |
7e8383a4-082c-4ffe-a4bc-c40fd3363c5ewith
the value of your SESSION cookie:
$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
Now visit the application at http://localhost:8080/ and observe that the attribute we added is no longer displayed.
Version 1.3.1.RELEASE
Last updated 2017-04-27 18:42:57 +00:00
相关文章推荐
- Spring Session+Spring Data Redis 解决分布式系统架构中 Session 共享问题
- spring-session,利用redis存储session,解决集群共享session的问题
- spring-session和redis解决spring cloud中session不一致性问题
- spring boot学习5之session+redis解决session共享问题
- 解决getCurrentSession()spring无法注入SessionFactory的问题
- 【Spring十】使用OpenSessionInView解决懒加载问题及Spring管理下的session和事务
- Spring Data Redis 二:RedisTemplate实现事物问题剖析和解决
- 利用tomcat-redis-session-manager解决tomcat的分布式session问题
- 添加@Transactional ------spring boot中session中断的问题及其解决
- 使用clone解决hibernate+spring集成中的延迟加载问题及分析(no session or session was closed)
- 使用clone解决hibernate+spring集成中的延迟加载问题及分析(no session or session was closed)
- Spring-data-mongodb中@Document的重要性(解决Mongodb与redis共存时无法注入Repository)
- Spring 和Struts2 整合 特别需要关注Session 关闭的问题:解决方法是使用 OpenSessionInViewFilter
- 利用redis存储session,解决登录同步问题
- 使用redis解决tomcat6在nginx负载下多节点共享session问题
- 使用redis解决tomcat6在nginx负载下多节点共享session问题
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- Spring3 + Hibernate4 配置 No Session found for current thread问题的解决
- jedis,spring-redis-data 整合使用,版本问题异常以及解决。
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题