Three methods for SpringSession to solve (cluster) session sharing problems

1. The same project (cluster environment) under the same domain name realizes session sharing

Deploying multiple tomcats in the same project is a cluster

A tomcat setting port number is: 9100

A tomcat setting port number is: 9200

applicationContext-session.xml configures the core code:

<!--spring session的配置类-->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        
</bean>

2. Realize session sharing for different projects with the same domain name

A tomcat setting port number is: 9100

A tomcat setting port number is: 9200

applicationContext-session.xml configures the core code: set the cookie to the same / root directory

<!--spring session的配置类-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!--设置cookie的存放方式-->
        <property name="cookieSerializer" ref="defaultCookieSerializer"/>
    </bean>

    <!--设置cookie的存放方式具体实现-->
    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <!--设置cookie的路径为 / 根路径:解决同域名不同项目的session共享问题-->
        <property name="cookiePath" value="/"/>
    </bean>

3. The same root domain name, different second-level sub-domain names, realize session sharing

A tomcat setting port number is: 9100

A tomcat setting port number is: 9200

applicationContext-session.xml configures the core code: set the same cookie path (the same project is ignored), the same root domain name: web.com

<!--spring session的配置类-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!--设置cookie的存放方式-->
        <property name="cookieSerializer" ref="defaultCookieSerializer"/>
    </bean>

    <!--设置cookie的存放方式具体实现-->
    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <!--设置cookie的路径为 / 根路径:解决同域名不同项目的session共享问题-->
        <property name="cookiePath" value="/"/>
        <!--同一个根域名,不同的二级子域名:
        解决session共享问题:第一步:设置同一个cookie的根路径
        第二步:设置同一个根域名,第三步:修改本地的hosts文件-->
        <property name="domainName" value="web.com"/>
    </bean>

Modify the local hosts file

The first access path: http://www.web.com:9100/p2p/setSession 

The second access path: http://beijing.web.com:9200/shop/getSession

Summary: Distinguish whether the same session is related to path and domain name

Guess you like

Origin blog.csdn.net/DDDM456/article/details/124428047