最开始的时候,一直在本地使用127.0.0.1来访问,两个项目是可以实现session共享的,后来加上子域名后,session就不能共享啦,百度啦一下说是要加上下面的配置:<bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">  
        <property name="domainName" value=".test.org"/>
        <property name="cookieName" value="JSESSIONID"/>
        <property name="cookiePath" value="/"></property>
        <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>-->
    </bean>但是这个配置一加上就有问题了:
An invalid domain [.test.org] was specified for this cookie
网上说是因为tomcat的问题,我用的tomcat9。换成tomcat7后,这个问题不存在啦,但是发现每次访问的sessionid都不同,导致我验证码都没法同步....
sessionID:9bb4bea7-391c-4150-a761-3d4eef672447
sessionID:38a5c418-d0a5-4dc0-947b-c0f4e1100b2b
sessionID:5bb83fcb-c02a-45ba-8cfc-64ffbede8dd6
目前的配置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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.3.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=".test.org"/>
        <property name="cookieName" value="JSESSIONID"/>
        <property name="cookiePath" value="/"></property>
        <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>-->
    </bean>
    
    <bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="3600" />
<property name="cookieSerializer" ref="defaultCookieSerializer"/>
</bean>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory" ref="jedisConnectionFactory" />  
        <property name="keySerializer">    
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="valueSerializer">    
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="hashKeySerializer">    
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="hashValueSerializer">    
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>   
    </bean> <bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="hostName" value="${redis_hostname}" />
<property name="port" value="${redis_port}" />
<property name="password" value="${redis_pwd}" />
<property name="timeout" value="3000" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>

<bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">  
    <constructor-arg name="listeners">
        <list>
            <bean class="com.test.web.SessionCounter" />
        </list>
    </constructor-arg>
</bean></beans> 如果没有配置defaultCookieSerializer,不用域名,使用IP加端口就能够实现session同步。
求解决方案