日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Session实战4

發布時間:2024/4/13 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Session实战4 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
然后來到Spring Session的配置文件,我們增加一個bean,id就是這個類defaultCookieSerializer,class就是org.springframework.session.web.http.DefaultCookieSerializer,我們現在要往里面注入屬性了,property節點,我們要把這個session名字改掉,不叫session了,domainName我們注入到一級域名下,現在我們要注入useHttpOnlyCookie,這個我們可以直接指定他為true,當然呢我們是使用的servlet3,所以默認的也是一個true,之前的cookie是存了一年,這個時間有點長,現在我們的配置是300秒,我們還是把它改成默認的30分鐘,這里我們也存一年,一起好看效果,現在這個屬性就注入好了 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"><property name="maxInactiveIntervalInSeconds" value="1800" /></bean><bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"><property name="domainName" value=".happymmall.com" /><property name="useHttpOnlyCookie" value="true" /><property name="cookiePath" value="/" /><property name="cookieMaxAge" value="31536000" /></bean><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="20"/></bean><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></beans> 我們再來檢查一下,看一下cookie

可以看到NAME已經變成了SESSION_NAME,是和我們原生的實現是一致了,domain也改成我們想要的,path也改成我們想要的,然后有效期也是我們想要的,要學會注入,根據實際的情況,來注入自己想要的一些屬性,當然我們代碼里面注入的是非常簡單的,在某些類里面呢,可以使用集合,構造器,就是非常靈活的,也是Spring框架的一個精髓所在,cookieName我們就不注入了,就使用默認的cookieName,我們在登陸的時候肯定會調用writeCookie/** (non-Javadoc)** @see org.springframework.session.web.http.CookieWriter#writeCookieValue(org.* springframework.session.web.http.CookieWriter.CookieValue)*/public void writeCookieValue(CookieValue cookieValue) {HttpServletRequest request = cookieValue.getRequest();HttpServletResponse response = cookieValue.getResponse();String requestedCookieValue = cookieValue.getCookieValue();String actualCookieValue = this.jvmRoute == null ? requestedCookieValue: requestedCookieValue + this.jvmRoute;Cookie sessionCookie = new Cookie(this.cookieName, actualCookieValue);sessionCookie.setSecure(isSecureCookie(request));sessionCookie.setPath(getCookiePath(request));String domainName = getDomainName(request);if (domainName != null) {sessionCookie.setDomain(domainName);}if (this.useHttpOnlyCookie) {sessionCookie.setHttpOnly(true);}if ("".equals(requestedCookieValue)) {sessionCookie.setMaxAge(0);}else {sessionCookie.setMaxAge(this.cookieMaxAge);}response.addCookie(sessionCookie);}看看這個是怎么做的,首先他從cookieValue里面拿了request和response,而cookieValue我們看一下,request是經過包裝之后的request

response也是一樣的,requestedCookieValue是46db,也就是將要寫cookie的一個value值,這里面又對jvmRoute進行了一個判斷,然后拿到真實的cookieValue,真的cookieValue和requestValue是一樣的

具體的細節也可以來getDomainName里面來看private String getDomainName(HttpServletRequest request) {if (this.domainName != null) {return this.domainName;}if (this.domainNamePattern != null) {Matcher matcher = this.domainNamePattern.matcher(request.getServerName());if (matcher.matches()) {return matcher.group(1);}}return null;}因為我們是注入進來的,這個肯定是不會為空的,如果不注入就會走到下面的if,通過正則獲取domainName,那如果正則也沒有拿到,最后返回的就是一個null,sessionCookie是Cookie類型 這里有一個readCookieValues/** (non-Javadoc)** @see org.springframework.session.web.http.CookieSerializer#readCookieValues(javax.* servlet.http.HttpServletRequest)*/public List<String> readCookieValues(HttpServletRequest request) {Cookie[] cookies = request.getCookies();List<String> matchingCookieValues = new ArrayList<String>();if (cookies != null) {for (Cookie cookie : cookies) {if (this.cookieName.equals(cookie.getName())) {String sessionId = cookie.getValue();if (sessionId == null) {continue;}if (this.jvmRoute != null && sessionId.endsWith(this.jvmRoute)) {sessionId = sessionId.substring(0,sessionId.length() - this.jvmRoute.length());}matchingCookieValues.add(sessionId);}}}return matchingCookieValues;}從request里面獲取cookie的值,而這個request就是被包裝好的,然后看一下cookie的值,里面有一個cookie,name是SESSION,value就是這個seeionId,首先new了一個空的集合,看這個名字就知道

?

總結

以上是生活随笔為你收集整理的Spring Session实战4的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。