1. Add dependencies
<Dependency><Groupid>org.springframework.session</Groupid><Artifactid>spring-session-data-redis</artifactid> <version>1.2.1.release</version></Dependency ><dependency> <groupid>redis.clients</groupid> < Artifactid>jedis</artifactid> <version>2.8.1</version> </dependency>
2. Configuration
Spring-mvc.xml:
<bean id="Redishttpsessionconfiguration"class="Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" > < Property Name="Maxinactiveintervalinseconds"Value="/></bean><bean" id="Jedispoolconfig"class="Redis.clients.jedis.JedisPoolConfig" > <property name="Maxtotal"Value="/>" <property name="Maxidle"Value="Ten"/></bean><bean id="Jedisconnectionfactory"class="Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method= "destroy" > <property name= "hostName" value=" ${redis_hostname} "/> <property name= "port" value= "${redis_port}"/ > <property name= "password" value= "${redis_pwd}"/> <property name= "timeout" value="/> <property name= "UsePool" Span class= "Hljs-keyword" >value= "true"/> <property name= "poolconfig" ref= " Jedispoolconfig "/></BEAN>
Web. XML Add Interceptors:
<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>
3. Using Spring-session
As long as the session is invoked using the standard Servlet API, it will be available at the bottom of the spring session and will be stored in Redis or other data sources of your choice.
Here is a demo I wrote:
/** *@author FENGZP *@date 17/2/23 3:19 *@email [email protected] *@company Guangzhou Yi Zhan Tong Computer Technology Co., Ltd. * *@Controller@RequestMapping (value ="Index")PublicClassIndexcontroller {PrivateFinal Gson Gson =New Gsonbuilder (). Setdateformat ("Yyyymmddhhmmss"). Create ();@RequestMapping (value ="Login")Public Stringlogin (httpservletrequest request, String username) { Request.getsession (). SetAttribute ( "user", Gson.tojson (new User (Username, "123456"))); return "login";} @RequestMapping (value = "index") < Span class= "Hljs-keyword" >public String index ( HttpServletRequest request, model model) {User user = Gson.fromjson (request.getsession (). getattribute (" user "). ToString (), user.class); Model.addattribute ( "user", user); return "index";}}
Spring-session+redis implementing Session sharing