前面有用 tomcat-redis-session-manager來實現(xiàn)分布式session管理,但是它有一定的局限性,主要是跟tomcat綁定太緊了,這里改成用Spring Session來管理分布式session,Spring Session就完全實現(xiàn)了與具體的容器無關(guān),如果需要了解如何用tomcat-redis-session-manager實現(xiàn)分分布式session,請看我之前的文章,下面正式進入主題,Spring Session項目搭建。
1. 引入Spring Session maven依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!-- 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. Spring配置文件中添加Spring Session相關(guān)配置(這里重點體現(xiàn)Spring Session,因此并沒有列出redis相關(guān)配置,需要可參考實例代碼)
1
2
3
4
5
6
|
<!-- 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. 在web.xml中配置Spring Session的Filter,它必須放在所有Filter的前面
1
2
3
4
5
6
7
8
9
|
<!-- 添加一個session代理filter,來包裝Servlet的getSession,需要放在所有filter鏈最前面 --> <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> |
這幾乎就是所有的步驟了,是不是感覺很簡單呢?趕快自己動手試一試吧,看起來高大上的分布式Session就這樣被Spring Session搞定了!
下面是我的github源碼地址:
https://github.com/13babybear/bounter-springsession
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/gdufs/p/6833270.html