原生的單點(diǎn)登錄邏輯會(huì)對(duì)我們的代碼產(chǎn)生污染善炫,一旦業(yè)務(wù)需求發(fā)生變更,改換成另外的方案美澳,那么就會(huì)非常的麻煩销部。更好的解決方案就是Spring-Session,她的可插拔性非常好制跟,當(dāng)然舅桩,這只是其中的一個(gè)優(yōu)點(diǎn),我們開始吧
概述
- SpringSession提供了
- 一套管理ServletHttpSession的解決方案雨膨;
- 一臺(tái)Cluster Session的解決方案擂涛,默認(rèn)使用外置的Redis存儲(chǔ)Session的方式解決Session共享的問題;
- 相關(guān)網(wǎng)站:
集成步驟
- 導(dǎo)入maven(注意Spring的版本需要大于等于4.0.3,否在后面的過濾器會(huì)報(bào)異常):
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.0.RELEASE</version> </dependency>
- 添加過濾器
SessionExpireFilter
<filter> <filter-name>sessionExpireFilter</filter-name> <filter-class>com.mmall.controller.common.SessionExpireFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionExpireFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
- 在Spring的配置文件中添加配置:
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800" /> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="20"/> <property name="maxIdle" value="10"/> <property name="minIdle" value="2"/> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="false"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1" /> <property name="port" value="6379" /> <property name="database" value="0"/> <property name="poolConfig" ref="jedisPoolConfig" /> </bean>
- 添加單點(diǎn)登錄Session的各種配置:
<bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"> <property name="cookieName" value="USER_KEY"/> <property name="domainName" value=".shreker.org"/> <property name="useHttpOnlyCookie" value="true"/> <!-- Only In Servlet3 --> <property name="cookiePath" value="/"/> <property name="cookieMaxAge" value="1800"/> </bean>
- 正常編寫session代碼(注意:這里的response.getData()返回的對(duì)象必須實(shí)現(xiàn)接口
java.io.Serializable
)撒妈,如:session.setAttribute(Consts.USER_IN_SESSION,response.getData()); session.getAttribute(Const.USER_IN_SESSION); session.removeAttribute(Const.USER_IN_SESSION);
- 完成配置恢暖,需要注意:
- Spring-Session通過代理攔截我們?cè)O(shè)置的session信息,并使用Jedis存儲(chǔ)到Redis上狰右;
- Spring-Session在Redis上存儲(chǔ)的不止用戶的信息杰捂,還有兩個(gè)關(guān)聯(lián)的key;
- 當(dāng)過期時(shí)間到時(shí)棋蚌,Spring-Session會(huì)先去刪除關(guān)聯(lián)的key信息嫁佳,最后過一段時(shí)間刪除真正存儲(chǔ)的用戶信息;
- Redis中谷暮,當(dāng)關(guān)聯(lián)的key被刪除之后蒿往,真正的信息刪除之前的這一段時(shí)間里,我們也是無(wú)法獲取真正存儲(chǔ)在Redis中的信息的湿弦;
- 使用到的相關(guān)的類:
- org.springframework.data.redis.connection.jedis.JedisConnectionFactory
- org.springframework.web.filter.DelegatingFilterProxy
- org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
- org.springframework.session.web.http.DefaultCookieSerializer
- redis.clients.jedis.JedisPoolConfig
- org.springframework.session.web.http.SessionRepositoryFilter
- org.springframework.session.data.redis.RedisOperationsSessionRepository
- org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer
- org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryRequestWrapper
- org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryResponseWrapper
- org.springframework.session.web.http.CookieHttpSessionStrategy