Spring Session 實現分散式會話管理
1、分散式會話管理是什麼?
在Web專案開發中,會話管理是一個很重要的部分,用於儲存與使用者相關的資料。通常是由符合session規範的容器來負責儲存管理,也就是一旦容器關閉,重啟會導致會話失效。因此打造一個高可用性的系統,必須將session管理從容器中獨立出來。
2、分散式會話管理的解決方案選用
實現方案有很多種,下面簡單介紹下:
第一種是使用容器擴充套件來實現,大家比較容易接受的是通過容器外掛來實現,比如基於Tomcat的tomcat-redis-session-manager,基於Jetty的jetty-session-redis等等。好處是對專案來說是透明的,無需改動程式碼。不過前者目前還不支援Tomcat 8,或者說不太完善。個人覺得由於過於依賴容器,一旦容器升級或者更換意味著又得從新來過。並且程式碼不在專案中,對開發者來說維護也是個問題。
第二種是自己寫一套會話管理的工具類,包括Session管理和Cookie管理,在需要使用會話的時候都從自己的工具類中獲取,而工具類後端儲存可以放到Redis中。很顯然這個方案靈活性最大,但開發需要一些額外的時間。並且系統中存在兩套Session方案,很容易弄錯而導致取不到資料。
第三種是使用框架的會話管理工具,也就是本文要說的spring-session,可以理解是替換了Servlet那一套會話管理,既不依賴容器,又不需要改動程式碼,並且是用了spring-data-redis那一套連線池,可以說是最完美的解決方案。當然,前提是專案要使用Spring Framework才行。
3、為什麼使用Spring Session
Spring Session為企業級Java應用的session管理帶來了革新,使得以下的功能更加容易實現:
- 將session所儲存的狀態解除安裝到特定的外部session儲存中,如Redis或Apache Geode中,它們能夠以獨立於應用伺服器的方式提供高質量的叢集。
- 當用戶使用WebSocket傳送請求的時候,能夠保持HttpSession處於活躍狀態。
- 在非Web請求的處理程式碼中,能夠訪問session資料,比如在JMS訊息的處理程式碼中。
- 支援每個瀏覽器上使用多個session,從而能夠很容易地構建更加豐富的終端使用者體驗。
- 控制session id如何在客戶端和伺服器之間進行交換,這樣的話就能很容易地編寫Restful API,因為它可以從HTTP 頭資訊中獲取session id,而不必再依賴於cookie。
參考:http://m.blog.csdn.net/article/details?id=51483471
4、Spring Session分散式會話解決方案
web.xml配置:
<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>
編寫配置,註解@EnableRedisHttpSession通過Import,引入了RedisHttpSessionConfiguration配置類。該配置類通過@Bean註解,向Spring容器中註冊了一個SessionRepositoryFilter(SessionRepositoryFilter的依賴關係:SessionRepositoryFilter --> SessionRepository --> RedisTemplate --> RedisConnectionFactory)。
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisHttpSessionConfig {
@Bean
public RedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(6379);
connectionFactory.setHostName("127.0.0.1");
return connectionFactory;
}
}
更復雜的Java Confg,需要加入spring 容器。
package io.flysium.framework.session;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
/**
* Spring Session分散式會話解決方案
*
* @author SvenAugustus(蔡政灤) e-mail: [email protected]
* @version 1.0
*/
@Configuration
@EnableScheduling
public class RedisHttpSessionConfig extends RedisHttpSessionConfiguration {
/**
* Spring Data Redis 的連線工廠配置,必選
*/
@Bean(name = "connectionFactory")
public RedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(6379);
connectionFactory.setHostName("127.0.0.1");
return connectionFactory;
}
/**
* Spring Data Redis 的會話儲存倉庫配置,可選
*/
@Bean(name = "sessionRepository")
public RedisOperationsSessionRepository sessionRepository(
RedisOperations<Object, Object> sessionRedisTemplate,
ApplicationEventPublisher applicationEventPublisher) {
this.setMaxInactiveIntervalInSeconds(Integer.valueOf(900)); // 單位:秒
this.setRedisNamespace(getApplicationName());
this.setRedisFlushMode(RedisFlushMode.ON_SAVE);
return super.sessionRepository(sessionRedisTemplate, applicationEventPublisher);
}
/**
* Spring Data Redis 的預設序列化工具,可選
*/
@Bean(name = "springSessionDefaultRedisSerializer")
public RedisSerializer springSessionDefaultRedisSerializer() {
RedisSerializer defaultSerializer = new JdkSerializationRedisSerializer();
//RedisSerializer defaultSerializer = new FastJsonStringRedisSeriaziler(
// Charset.forName("utf-8"));
return defaultSerializer;
}
/**
* Session會話策略配置,可選
*
* 1、Spring Session 預設支援Cookie儲存當前session的id,
* 即CookieHttpSessionStrategy。
* 2、Spring Session 支援RESTFul APIS,響應頭回返回x-auth-token,來標識當前session的token,
* 即HeaderHttpSessionStrategy。
*/
/*@Bean(name = "httpSessionStrategy")
public HttpSessionStrategy httpSessionStrategy() {
HeaderHttpSessionStrategy headerHttpSessionStrategy = new HeaderHttpSessionStrategy();
headerHttpSessionStrategy.setHeaderName("Auth-Token");
return headerHttpSessionStrategy;
}*/
/**
* Session會話策略為 CookieHttpSessionStrategy 情況下配置的 Cookie序列化工具,可選
*/
@Bean(name = "cookieSerializer")
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
// Cookie名稱
cookieSerializer.setCookieName(
new StringBuilder(getApplicationName()).append("SESSION").toString());
// HttpOnly
cookieSerializer.setUseHttpOnlyCookie(true);
// HTTPS定義
//cookieSerializer.setUseSecureCookie(true);
// 解決子域問題:把cookiePath的返回值設定為統一的根路徑就能讓session id從根域獲取,
//這樣同根下的所有web應用就可以輕鬆實現單點登入共享session
cookieSerializer.setCookiePath("/");
return cookieSerializer;
}
private String getApplicationName() {
return "app";
}
}
5、如何檢視session資料?
(1)Http Session資料(spring:session:名稱空間:sessions:xxxx)在Redis中是以Hash結構儲存的。
(2)Http Session過期資料(spring:session:名稱空間:expirations:xxxx)以Set結構儲存的。
記錄了所有session資料應該被刪除的時間(即最新的一個session資料過期的時間)。
6、Spring Session原理講解
所有的request都會經過SessionRepositoryFilter,而 SessionRepositoryFilter是一個優先順序最高的javax.servlet.Filter,它使用了一個SessionRepositoryRequestWrapper類接管了Http Session的建立和管理工作。
public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {
public SessionRepositoryRequestWrapper(HttpServletRequest original) {
super(original);
}
public HttpSession getSession() {
return getSession(true);
}
public HttpSession getSession(boolean createNew) {
// create an HttpSession implementation from Spring Session
}
// ... other methods delegate to the original HttpServletRequest ...
}