當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Session实战4
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Session实战3
- 下一篇: Spring Session源码解析