Spring Project Integration spring-session and problems that may be encountered
I. Preparing JAR packs
<!--Spring session Start-->
<dependency>
<groupid>org.springframework.session</ groupid>
<artifactId>spring-session</artifactId>
<version>1.1.0.release</ version>
</dependency>
<dependency>
<groupid>org.springframework.data</ groupid>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.release</ version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</ Dependency>
<!--Spring Session end-->
Non-Maven download, need to pay attention to more next Commons-pool2-2.3.jar
MAVEN does not have to download, MAVEN has automatically resolved to rely on two. Install Redis
My machine is windows, so I downloaded the version of Windows. Redis official only supports Linux, here is the version of Windows developed by Ms Open Tech, address in this redis for Windows, Remember to download the zip version , the latest version of 3.0.501, down to see
After decompression, the contents are as follows (the document was deleted for me). Don't care)
Note that this start.bat was built by myself and reads as follows:
Redis-server
Click Start.bat can start Redis, the specific configuration does not say, own Google three. Integrated into Spring project 1.Spring config file
<!--spring-session configuration started--> <bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig"/> <bean id= "Jedisconnectionfactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" hostName "value=" 1
27.0.0.1 "/> <property name=" "Port" value= "6379"/> <property name= "password" value= ""/>
<property name= "Timeout" value= "3600"/> <property name= "poolconfig" ref= "Jedispoolconfig"/> <property name= "Usepool" value= "true"/> </bean> <bean id= "redistemplate" class= "ORG.SPRINGF" Ramework.data.redis.core.StringRedisTemplate "> <property name=" connectionfactory "ref=" Jedisconnectionfacto Ry "/> </bean> <!--place session into Redis--> <bean id=" redishttpsessionconfiguration "class=" org
. springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration "> <property name= "Maxinactiveintervalinseconds" value= "1800"/> </bean> <!--spring-session configuration End-->
2.web.xml Configuration
Note: Please configure this filter to be the first place in all filters
<!--Spring session configuration start-->
<filter>
<filter-name>springsessionrepositoryfilter</ Filter-name>
<filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class >
</filter>
<filter-mapping>
<filter-name>springsessionrepositoryfilter</ filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring Session Configuration End-->
So far, Spring-session has been integrated into the Spring project four. Problems that may be encountered 1.session.setattribute ()
If you want to save an object in session, it will be an error if you write the following directly
Session.setattribute ("Users", user)
If you want to save the object, there is a prerequisite that the user class must implement the serializable interface so that Spring-session can serialize the user and store it in Redis. Five. Reference materials
1. Using spring session to do distributed conversation management Six. Supplement (2016.3.15)
A recent project is a restful project built using jersey, and it is impossible to use cookies to develop a client interface to a non-web. Fortunately, the spring session also supports another way: Store SessionID in Httpheader, This allows you to get sessionid without using cookies to go back to session
1. Modify the spring configuration file
<!--spring-session configuration started--> <bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig"/> <!--replaces the default use of cookies, which uses httpheader--> <bean id= "Httpsessonstrategy" class=. Web.http.HeaderHttpSessionStrategy "/> <bean id=" jedisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" hostName "value=" 1
27.0.0.1 "/> <property name=" "Port" value= "6379"/> <property name= "password" value= ""/>
<property name= "Timeout" value= "3600"/> <property name= "poolconfig" ref= "Jedispoolconfig"/> <property name= "Usepool" value= "true"/> </bean> <bean id= "redistemplate" class= "ORG.SPRINGF" Ramework.data.redis.core.StringRedisTemplate "> <property name=" connectionfactory "ref=" Jedisconnectionfacto Ry "/> </bean> <!--put the session into Redis--> <bean id= "redishttpsessionconfiguration" class= " Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration "> < Property Name= "Maxinactiveintervalinseconds" value= "1800"/> <property name= "httpsessionstrategy" ref= "HttpS Essonstrategy "/> </bean> <!--spring-session configuration End-->
2. Add filter, this is the same as the above web.xml configuration, do not paste code
3. Test
First, define a controller that contains a set and a get, simulate storage, and take out the session
/**
* @author: amayadream
* @date: 2016.03.10 19:05 * *
@Controller
@RequestMapping ( Value = ' test ') public
class testcontroller{
@RequestMapping (value = ' Set ') public
String Set ( HttpSession session) {
session.setattribute ("userid", "Amayadream");
Return "index";
@RequestMapping (value = ' get ')
@ResponseBody public
String Get (HttpSession session) {return
"Hello," + Session.getattribute ("userid");
}
Index.jsp is as follows
<%@ page contenttype= "Text/html;charset=utf-8" language= "java" pageencoding= utf-8 "%>" <%@ taglib prefix
= "C" uri= "Http://java.sun.com/jsp/jstl/core"%> <c:set var= "CTX" value= "${"
PageContext.request.contextPath} "/>
To start the project, here I use the restclient Firefox plugin to test
①set, here you can see the result is successful (jump to the index, no screenshots), and the header has a x-auth-token, its value is the corresponding SessionID
②get, here we add headers to the request and see if we can get the value.
Obviously, the value is not taken, and the value of the X-auth-token is changed, which is equivalent to reassigning you to a session
③get, here at the request, bring the SessionID in the ①.
In this way, the value is successfully fetched