spring session同域下單點登錄實現(xiàn)解析
Session會話管理
在Web項目開發(fā)中,Session會話管理是一個很重要的部分,用于存儲與記錄用戶的狀態(tài)或相關(guān)的數(shù)據(jù);通常情況下session交由容器(tomcat)來負責存儲和管理,但是如果項目部署在多臺tomcat中,則session管理存在很大的問題;
1、多臺tomcat之間無法共享session,比如用戶在tomcat A服務(wù)器上已經(jīng)登錄了,但當負載均衡跳轉(zhuǎn)到tomcat B時,由于tomcat B服務(wù)器并沒有用戶的登錄信息,session就失效了,用戶就退出了登錄;
2、一旦tomcat容器關(guān)閉或重啟也會導(dǎo)致session會話失效;因此如果項目部署在多臺tomcat中,就需要解決session共享的問題;
配置文件
pom.xml
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
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>*.do</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
applicationContext.xml
<context:annotation-config/>
<!-- 初始化一切spring-session準備,且把springSessionFilter放入IOC -->
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="300"/>
</bean>
<!-- 配置cookie信息-->
<bean class="org.springframework.session.web.http.DefaultCookieSerializer" id="defaultCookieSerializer">
<property name="cookieName" value="SESSION_NAME"/>
<property name="domainName" value="wangjun.com"/>
<property name="useHttpOnlyCookie" value="true"/>
<property name="cookiePath" value="/"/>
<property name="cookieMaxAge" value="31536000"/>
</bean>
<!-- 配置redis連接池信息-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
</bean>
<!--配置redis連接信息-->
<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="poolConfig" ref="jedisPoolConfig"/>
</bean>
代碼測試
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sesssionID = request.getSession().getId();
//部署兩份,把這個地方8081改成8080就行了,只是為了區(qū)分
response.getWriter().write("8081 Server SessionID"+sesssionID);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項目啟動不加載resources目錄下的文件問題
這篇文章主要介紹了Springboot項目啟動不加載resources目錄下的文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
從Hello?World開始理解GraphQL背后處理及執(zhí)行過程
這篇文章主要為大家介紹了從Hello?World開始理解GraphQL背后處理過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
java基于QuartzJobBean實現(xiàn)定時功能的示例代碼
QuartzJobBean是Quartz框架中的一個抽象類,用于定義和實現(xiàn)可由Quartz調(diào)度的作業(yè),本文主要介紹了java基于QuartzJobBean實現(xiàn)定時功能的示例代碼,具有一定的參考價值,感興趣可以了解一下2023-09-09
出現(xiàn)java.lang.UnsupportedClassVersionError錯誤的原因以及解決方法
這篇文章主要給大家介紹了關(guān)于出現(xiàn)java.lang.UnsupportedClassVersionError錯誤的原因以及解決方法,文中通過圖文以及代碼示例將這個錯誤介紹的非常詳細,需要的朋友可以參考下2024-05-05
Java Builder Pattern建造者模式詳解及實例
這篇文章主要介紹了Java Builder Pattern建造者模式詳解及實例的相關(guān)資料,需要的朋友可以參考下2017-01-01

