最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring session實(shí)現(xiàn)Session共享

 更新時(shí)間:2023年04月14日 14:41:12   作者:Sivan_Xin  
本文主要介紹了Spring session實(shí)現(xiàn)Session共享,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Redis session

用戶第一次訪問應(yīng)用,會創(chuàng)建一個(gè)新的Session,并且會將Session的ID作為Cookie緩存在瀏覽器。下一次訪問請求時(shí),請求的頭部會帶有Cookie。應(yīng)用通過Session ID進(jìn)行查找。如果Session存在并且有效,就會繼續(xù)請求。

為什么使用session共享:

如果沒有Session共享,session的信息放在內(nèi)存中,如果Tomcat關(guān)閉,內(nèi)存中的Session就會銷毀。在多實(shí)例(多個(gè)Tomcat)中無法共享,導(dǎo)致一個(gè)用戶只能訪問一個(gè)實(shí)例。

在實(shí)現(xiàn)共享后,只要Cookie中的Session ID 無法改變,多實(shí)例的任意一個(gè)被銷毀,都不會影響用戶訪問。

  • 有了session共享,即使服務(wù)器重啟也不需要重新登錄。
  • 假設(shè)某個(gè)網(wǎng)站是由多臺服務(wù)器提供服務(wù),nginx采用輪詢機(jī)制做負(fù)載均衡,那么同一個(gè)IP訪問該網(wǎng)站時(shí),請求就可能會被分配到不同的服務(wù)器上,如果 session 沒有 實(shí)現(xiàn)共享 ,就會出現(xiàn)重復(fù)登陸授權(quán)的情況。
  • 注意,瀏覽器不能關(guān)閉,因?yàn)闉g覽器的cookie是會話級別的,瀏覽器關(guān)閉就會失效。

session 共享原理:

當(dāng)請求進(jìn)來的時(shí)候,SessionRepositoryFilter 會先攔截到請求,將 request 和 response 對象轉(zhuǎn)換成 SessionRepositoryRequestWrapper 和 SessionRepositoryResponseWrapper 。后續(xù)當(dāng)?shù)谝淮握{(diào)用 request 的getSession方法時(shí),會調(diào)用SessionRepositoryRequestWrapper 的getSession方法。這個(gè)方法是被重寫過的,邏輯是先從 request 的屬性中查找,如果找不到;再查找一個(gè)key值是"SESSION"的 Cookie,通過這個(gè) Cookie 拿到 SessionId 去 Redis 中查找,如果查不到,就直接創(chuàng)建一個(gè)RedisSession 對象,同步到 Redis 中。

說的簡單點(diǎn)就是:攔截請求,將之前在服務(wù)器內(nèi)存中進(jìn)行 Session 創(chuàng)建銷毀的動作,改成在 Redis 中創(chuàng)建。

Spring Session提供了一套創(chuàng)建和管理Servlet HTTPSession的方案,Spring Session提供了集群Session功能,默認(rèn)采用外置的Redis來存儲Session數(shù)據(jù),以此來解決Session共享問題。

使用過濾器攔截請求

<!-- spring session共享filter -->
  <!-- 該過濾器必須是第一個(gè)過濾器,所有的請求經(jīng)過該過濾器后執(zhí)行后續(xù)操作 -->
  <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>

配置Redis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans
? ? ? ? ? ? ? ? ? ? ? ? ?http://www.springframework.org/schema/beans/spring-beans.xsd">

? ? <!--Jedis連接池的相關(guān)配置-->
? ? <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
? ? ? ? <!--最大連接數(shù), 默認(rèn)8個(gè)-->
? ? ? ? <property name="maxTotal" value="100"></property>
? ? ? ? <!--最大空閑連接數(shù), 默認(rèn)8個(gè)-->
? ? ? ? <property name="maxIdle" value="50"></property>
? ? ? ? <!--允許借調(diào) 在獲取連接的時(shí)候檢查有效性, 默認(rèn)false-->
? ? ? ? <property name="testOnBorrow" value="true"/>
? ? ? ? <!--允許歸還 在return給pool時(shí),是否提前進(jìn)行validate操作-->
? ? ? ? <property name="testOnReturn" value="true"/>
? ? </bean>
? ? <!--配置JedisConnectionFactory-->
? ? <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
? ? ? ? <property name="hostName" value="192.168.175.100"/>
? ? ? ? <property name="port" value="6379"/>
? ? ? ? <property name="database" value="0"/>
? ? ? ? <property name="poolConfig" ref="jedisPoolConfig"/>
? ? </bean>
? ? <!-- 配置session共享 -->
? ? <bean id="redisHttpSessionConfiguration"
? ? ? ? ? class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
? ? ? ? <property name="maxInactiveIntervalInSeconds" value="600" />
? ? </bean>
</beans>

到此這篇關(guān)于Spring session實(shí)現(xiàn)Session共享的文章就介紹到這了,更多相關(guān)Spring session 共享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

濮阳市| 眉山市| 重庆市| 乐昌市| 额尔古纳市| 沅陵县| 颍上县| 年辖:市辖区| 施秉县| 易门县| 宾阳县| 同心县| 安龙县| 德保县| 磴口县| 习水县| 青浦区| 行唐县| 灯塔市| 乌鲁木齐县| 沁水县| 拜城县| 定安县| 神农架林区| 白朗县| 安福县| 应城市| 康平县| 盐城市| 安宁市| 商都县| 平山县| 饶阳县| 新建县| 方正县| 潮安县| 锡林浩特市| 丰顺县| 大洼县| 长岛县| 紫金县|