Simple Example of Spring Session to implement distributed session, springsession example

Source: Internet
Author: User

Simple Example of Spring Session to implement distributed session, springsession example

Previously, tomcat-redis-session-manager was used to implement distributed session management, but it has some limitations, mainly because it is too tight to bind with tomcat, here, we use Spring Session to manage distributed sessions. Spring session is completely unrelated to specific containers. If you need to know how to use tomcat-redis-Session-manager to implement distributed sessions, please refer to my previous article. The following is the topic for setting up the Spring Session project.

 

1. Introduce Spring Session maven dependency

        <!-- spring session begin -->        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>2.9.0</version>        </dependency>        <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.3.1.RELEASE</version>        </dependency>        <!-- spring session end -->            

 

2. Add Spring Session-related configurations to the Spring configuration file (Spring Session is highlighted here, So redis-related configurations are not listed. You need to refer to the instance code)

    <!-- Spring Session begin -->    <bean id="redisHttpSessionConfiguration"        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">        <property name="maxInactiveIntervalInSeconds" value="1800" />    </bean>    <!-- Spring Session end -->

 

3. Configure the Spring Session Filter in web. xml, which must be placed before all filters.

<! -- Add a session proxy filter to wrap the Servlet's getSession, it must be placed at the top of all filter chains --> <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 is almost all the steps, isn't it easy? Hurry up and try it on your own. It seems that the distributed Session on the tall architecture is handled by Spring Session!

The following is my github Source Code address:

Https://github.com/13babybear/bounter-springsession

Thank you for your valuable comments!

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.