Spring-session & redis 子域名共享session

Spring-session & redis 子域名共享session

例子:

  • a.example.com
  • b.example.com

Spring 版本 4.2.6.RELEASE
Spring-session Maven 依赖相关的包


    <dependency>
      <groupId>redis.clientsgroupId>
      <artifactId>jedisartifactId>
      <version>2.5.2version>
    dependency>
    <dependency>
      <groupId>org.springframework.datagroupId>
      <artifactId>spring-data-redisartifactId>
      <version>1.7.1.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframework.sessiongroupId>
      <artifactId>spring-session-data-redisartifactId>
      <version>1.2.0.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-pool2artifactId>
      <version>2.2version>
    dependency>

redis.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
       default-autowire="byName" default-lazy-init="true">

    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    bean>

    
    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <property name="domainName" value=".example.com"/>
        <property name="cookieName" value="JSESSIONID"/>
        
    bean>

    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.pass}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true" />
    bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    bean>

    
    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800" />
        <property name="cookieSerializer" ref="defaultCookieSerializer"/>
    bean>

beans>

以上配置完后,分别启动 a tomcat(a.example.com),b tomcat(b.example.com) 查看sessionid 是否已经一致了 : )

另外告诉大家一个点.有问题,上github 搜 issues 关键字 : )

https://github.com/spring-projects/spring-session/issues/297

参考资料:
Spring Session - Custom Cookie
关于spring session redis共享session的一个坑
利用spring session解决共享Session问题

转载请注明本帖地址:http://blog.csdn.net/beflyabot/article/details/51449315

你可能感兴趣的:(Spring,session,redis)