SoFunction
Updated on 2025-03-08

Spring session implements Session sharing

Redis session

The first time a user accesses the app, he will create a new session and cache the Session ID in the browser as a cookie. The next time you access the request, the request will have a cookie on the head. The application is searched through the Session ID. If the Session exists and is valid, the request will continue.

Why use session sharing:

If there is no Session sharing, the session information is placed in memory, and if Tomcat is closed, the session in memory will be destroyed. It is not possible to share in multiple instances (multiple Tomcats), resulting in only one user accessing one instance.

After sharing is implemented, as long as the Session ID in the cookie cannot be changed, any of the multiple instances will not affect user access.

  • With session sharing, you don't need to log in again even if the server restarts.
  • Assuming that a website is provided by multiple servers and nginx uses a polling mechanism for load balancing, then when the same IP accesses the website, the request may be assigned to different servers. If the session does not implement sharing, repeated login authorization will occur.
  • Note that the browser cannot be closed because the browser's cookies are session-level and the browser's closing will be invalid.

session sharing principle:

When the request comes in, the SessionRepositoryFilter will first intercept the request and convert the request and response objects into SessionRepositoryRequestWrapper and SessionRepositoryResponseWrapper. Subsequently, when the getSession method of request is called for the first time, the getSession method of SessionRepositoryRequestWrapper will be called. This method has been rewritten. The logic is to first look up from the request attribute, if it cannot be found; then look up a cookie with the key value of "SESSION", use this cookie to get the SessionId and search in Redis. If it cannot be found, create a RedisSession object and synchronize it to Redis.

To put it simply, intercept the request, change the previous session creation and destruction action in the server memory to create it in Redis.

Spring Session provides a set of solutions to create and manage Servlet HTTPSession. Spring Session provides cluster Session function, which uses external Redis to store Session data by default to solve the Session sharing problem.

Intercept requests using filters

<!-- spring sessionsharedfilter -->
  <!-- The filter must be the first filter,All requests pass through this filter and perform subsequent operations. -->
  <filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class></filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Configure Redis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance"
       xsi:schemaLocation="/schema/beans
                         /schema/beans/">

    <!--JedisConnection pool related configuration-->
    <bean  class="">
        <!--Maximum number of connections, default8indivual-->
        <property name="maxTotal" value="100"></property>
        <!--Maximum number of idle connections, default8indivual-->
        <property name="maxIdle" value="50"></property>
        <!--Removal is allowed Check validity when getting a connection, defaultfalse-->
        <property name="testOnBorrow" value="true"/>
        <!--Repayment is allowed existreturnGivepoolhour,Whether to proceed in advancevalidateoperate-->
        <property name="testOnReturn" value="true"/>
    </bean>
    <!--ConfigurationJedisConnectionFactory-->
    <bean  class="">
        <property name="hostName" value="192.168.175.100"/>
        <property name="port" value="6379"/>
        <property name="database" value="0"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>
    <!-- Configurationsessionshared -->
    <bean 
          class="">
        <property name="maxInactiveIntervalInSeconds" value="600" />
    </bean>
</beans>

This is the end of this article about Spring session sharing. For more related Spring session sharing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!