不同根域名下的項目實現Session共享,
比如阿里巴巴這樣的公司,有多個業務線,多個網站,用戶在一個網站登錄,那么其他網站也會是登錄了的狀態,比如:登錄了淘寶網,則天貓網也是登錄的;
www.taobao.com
www.tmall.com
比如:www.web.com www.p2p.com www.dai.com
對于不同根域名的場景,要實現一處登錄,處處登錄,Spring Session不支持;
單點登錄(Single Sign On),簡稱為 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。目前市面上有很多實現單點登錄的方案,例如CAS,Token頒發校驗,Cookie+域名+路徑配置,在這里主要是想介紹一下第三種方案的實現方式。
2. 相關網站:
• 官方網站:https://projects.spring.io/spring-session
• 文檔地址:https://docs.spring.io/spring-session/docs/current/reference/html5
• 源碼地址:https://github.com/spring-projects/spring-session
1. 導入maven(注意Spring的版本需要大于等于4.0.3,否在后面的過濾器會報異常):
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
2. 添加過濾器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>
3. 在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>
4. 添加單點登錄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>
5. 正常編寫session代碼(注意:這里的response.getData()返回的對象必須實現接口java.io.Serializable),如:
session.setAttribute(Consts.USER_IN_SESSION,response.getData());
session.getAttribute(Const.USER_IN_SESSION);
session.removeAttribute(Const.USER_IN_SESSION);
6. 完成配置,需要注意:
• Spring-Session通過代理攔截我們設置的session信息,并使用Jedis存儲到Redis上;
• Spring-Session在Redis上存儲的不止用戶的信息,還有兩個關聯的key;
• 當過期時間到時,Spring-Session會先去刪除關聯的key信息,最后過一段時間刪除真正存儲的用戶信息;
• Redis中,當關聯的key被刪除之后,真正的信息刪除之前的這一段時間里,我們也是無法獲取真正存儲在Redis中的信息的;
7. 使用到的相關的類:
• 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