本篇文章給大家分享的是有關(guān)怎么在spring中使用session實(shí)現(xiàn)同域下單點(diǎn)登錄,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Session會(huì)話管理
在Web項(xiàng)目開發(fā)中,Session會(huì)話管理是一個(gè)很重要的部分,用于存儲(chǔ)與記錄用戶的狀態(tài)或相關(guān)的數(shù)據(jù);通常情況下session交由容器(tomcat)來負(fù)責(zé)存儲(chǔ)和管理,但是如果項(xiàng)目部署在多臺(tái)tomcat中,則session管理存在很大的問題;
1、多臺(tái)tomcat之間無法共享session,比如用戶在tomcat A服務(wù)器上已經(jīng)登錄了,但當(dāng)負(fù)載均衡跳轉(zhuǎn)到tomcat B時(shí),由于tomcat B服務(wù)器并沒有用戶的登錄信息,session就失效了,用戶就退出了登錄;
2、一旦tomcat容器關(guān)閉或重啟也會(huì)導(dǎo)致session會(huì)話失效;因此如果項(xiàng)目部署在多臺(tái)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準(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>
代碼測試
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); } }
以上就是怎么在spring中使用session實(shí)現(xiàn)同域下單點(diǎn)登錄,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站aaarwkj.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:怎么在spring中使用session實(shí)現(xiàn)同域下單點(diǎn)登錄-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://aaarwkj.com/article26/dpsdjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、虛擬主機(jī)、定制開發(fā)、微信小程序、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容