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.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://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了一个空的集合,看这个名字就知道

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值