• 转:Spring-session & redis 子域名共享session


    Spring-session & redis 子域名共享session

    例子:

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

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

     
    <!-- redis -->
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.5.2</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.7.1.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.session</groupId>
          <artifactId>spring-session-data-redis</artifactId>
          <version>1.2.0.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-pool2</artifactId>
          <version>2.2</version>
        </dependency>

    Redis.xml

    <?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: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">
    
        <!-- redis -->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        </bean>
    
        <!-- 设置Cookie domain 和 名称 -->
        <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
            <property name="domainName" value=".example.com"/>
            <property name="cookieName" value="JSESSIONID"/>
            <!--<property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"/>-->
        </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>
    
        <!-- 将session放入redis -->
        <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

  • 相关阅读:
    Google maps not working IE11
    谷歌地图插件
    杨辉三角python的最佳实现方式,牛的不能再牛了
    今日头条推荐算法原理全文详解
    项目启动大会要点
    学金融应该看的书籍
    【从0到1】经典语录
    appium学习链接记录
    Axure-计算输入字数
    如何提升个人专业能力
  • 原文地址:https://www.cnblogs.com/1995hxt/p/6149016.html
Copyright © 2020-2023  润新知