spring session同域下單點(diǎn)登錄實(shí)現(xiàn)解析
Session會(huì)話管理
在Web項(xiàng)目開(kāi)發(fā)中,Session會(huì)話管理是一個(gè)很重要的部分,用于存儲(chǔ)與記錄用戶(hù)的狀態(tài)或相關(guān)的數(shù)據(jù);通常情況下session交由容器(tomcat)來(lái)負(fù)責(zé)存儲(chǔ)和管理,但是如果項(xiàng)目部署在多臺(tái)tomcat中,則session管理存在很大的問(wèn)題;
1、多臺(tái)tomcat之間無(wú)法共享session,比如用戶(hù)在tomcat A服務(wù)器上已經(jīng)登錄了,但當(dāng)負(fù)載均衡跳轉(zhuǎn)到tomcat B時(shí),由于tomcat B服務(wù)器并沒(méi)有用戶(hù)的登錄信息,session就失效了,用戶(hù)就退出了登錄;
2、一旦tomcat容器關(guān)閉或重啟也會(huì)導(dǎo)致session會(huì)話失效;因此如果項(xiàng)目部署在多臺(tái)tomcat中,就需要解決session共享的問(wèn)題;
配置文件
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準(zhǔn)備,且把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>
代碼測(cè)試
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();
//部署兩份,把這個(gè)地方8081改成8080就行了,只是為了區(qū)分
response.getWriter().write("8081 Server SessionID"+sesssionID);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
- 使用Spring Security OAuth2實(shí)現(xiàn)單點(diǎn)登錄
- SpringCloud實(shí)現(xiàn)SSO 單點(diǎn)登錄的示例代碼
- vue+springboot前后端分離實(shí)現(xiàn)單點(diǎn)登錄跨域問(wèn)題解決方法
- spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
- spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制
- 詳解spring boot配置單點(diǎn)登錄
相關(guān)文章
通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證的方法詳解
RSA是一種非對(duì)稱(chēng)加密算法,是目前廣泛應(yīng)用于加密和數(shù)字簽名領(lǐng)域的一種加密算法,本文主要講述如何通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證,應(yīng)用場(chǎng)景為與其他平臺(tái)對(duì)接接口時(shí),通過(guò)RSA加密和解密驗(yàn)證請(qǐng)求的有效性,在對(duì)接時(shí)雙方互換公鑰,需要的朋友可以參考下2023-12-12
spring boot整合quartz實(shí)現(xiàn)多個(gè)定時(shí)任務(wù)的方法
這篇文章主要介紹了spring boot整合quartz實(shí)現(xiàn)多個(gè)定時(shí)任務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Spring中的spring-retry重試機(jī)制解析
這篇文章主要介紹了Spring中的spring-retry重試機(jī)制解析,spring-retry可以通過(guò)注解,在不入侵原有業(yè)務(wù)邏輯代碼的方式下,優(yōu)雅的實(shí)現(xiàn)重處理功能,在spring-retry中,所有配置都是基于簡(jiǎn)單注釋的,需要的朋友可以參考下2024-01-01
SpringMvc使用GoogleKaptcha生成驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了SpringMvc項(xiàng)目中使用GoogleKaptcha 生成驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
關(guān)于HashSet與HashMap的區(qū)別及說(shuō)明
這篇文章主要介紹了關(guān)于HashSet與HashMap的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

