Using the spring session to do distributed session management

Source: Internet
Author: User
Tags sessions redis server

In Web project development, session management is an important part of storing data that is relevant to the user. Typically, a container that conforms to the session specification is responsible for storage management, which means that once the container is closed, a restart can invalidate the session. Therefore, to create a highly available system, session management must be isolated from the container. There are many ways to achieve this, and the following is a brief introduction:

The first is implemented using container extensions, which are easier to accept through container plug-ins, such as Tomcat-based Tomcat-redis-session-manager, jetty-based Jetty-session-redis, and so on. The benefits are transparent to the project, without the need to change the code. However, the former does not currently support Tomcat 8, or is not perfect. Personally feel that because of too much reliance on the container, once the container upgrade or replacement means that it has to be new. And the code is not in the project, and maintenance is a problem for developers.

The second is to write a set of tool classes for session management, including session management and Cookie management, which are obtained from your own tool classes when you need to use the session, while the tool class backend storage can be placed in Redis. It is clear that this scheme is the most flexible, but development takes some extra time. And there are two sets of sessions in the system, it is easy to mistake and lead to the data can not be taken.

The third is the use of the framework of the Session management tool, which is what this article spring-session, can be understood to replace the servlet of the set of session management, neither rely on the container, do not need to change the code, and is used Spring-data-redis that set of connection pool, Can be said to be the most perfect solution. Of course, the premise is that the project uses the Spring framework.

Here is a simple record of the integration process:

If the project has not been previously integrated with Spring-data-redis, this step needs to be done first, adding these two dependencies in Maven:

<dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-redis</artifactId>    <version>1.5.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework.session</groupId>    <artifactId>spring-session</artifactId>    <version>1.0.2.RELEASE</version></dependency>

Add the following bean to the Applicationcontext.xml to define the connection pool for Redis and initialize the Redis template action classes, replacing the related variables in them yourself.

<!-- redis --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean><bean id="jedisConnectionFactory"    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <property name="hostName" value="${redis.host}" />    <property name="port" value="${redis.port}" />    <property name="password" value="${redis.pass}" />    <property name="timeout" value="${redis.timeout}" />    <property name="poolConfig" ref="jedisPoolConfig" />    <property name="usePool" value="true" /></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">    <property name="connectionFactory" ref="jedisConnectionFactory" /></bean><!-- 将session放入redis --><bean id="redisHttpSessionConfiguration"class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">    <property name="maxInactiveIntervalInSeconds" value="1800" /></bean>

Here are a few beans are used in the operation of Redis, the last Bean is spring-session need to use, where the ID can not write or remain unchanged, which is a contractual priority configuration of the embodiment. This bean also automatically generates multiple beans for related operations, greatly simplifying our configuration items. One of the more important is springsessionrepositoryfilter, which will be called in the proxy filter below. Maxinactiveintervalinseconds indicates a time-out, which defaults to 1800 seconds. I am personally accustomed to using XML to define the above configuration, and there are annotations in the official documentation to declare a configuration class.

Then, add a session proxy filter in Web. XML to wrap the servlet's getsession () through this filter. Note that this filter needs to be placed at the front of all filter chains.

<!-- delegatingFilterProxy --><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>

This completes the configuration, and it is important to note that the spring-session requires a Redis server version of no less than 2.8.

Verification: The session key can be viewed using REDIS-CLI, and the Jsessionid in the browser cookie has been replaced with the session.

127.0.0.1:6379> KEYS *1) "spring:session:expirations:1440922740000"2) "spring:session:sessions:35b48cb4-62f8-440c-afac-9c7e3cfe98d3"

This article link address: http://dorole.com/1422/

Using the spring session to do distributed session management

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.