Spring-session+redis Implementing Session Sharing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.