【4.7】使用Spring session实现Session共享

Redis session

用户第一次访问应用,会创建一个新的Session,并且会将Session的ID作为Cookie缓存在浏览器。下一次访问请求时,请求的头部会带有Cookie。应用通过Session ID进行查找。如果Session存在并且有效,就会继续请求。

  • 为什么使用session共享:

    如果没有Session共享,session的信息放在内存中,如果Tomcat关闭,内存中的Session就会销毁。在多实例(多个Tomcat)中无法共享,导致一个用户只能访问一个实例。

    在实现共享后,只要Cookie中的Session ID 无法改变,多实例的任意一个被销毁,都不会影响用户访问。

    • 有了session共享,即使服务器重启也不需要重新登录。

    • 假设某个网站是由多台服务器提供服务,nginx采用轮询机制做负载均衡,那么同一个IP访问该网站时,请求就可能会被分配到不同的服务器上,如果 session 没有 实现共享 ,就会出现重复登陆授权的情况

    • 注意,浏览器不能关闭,因为浏览器的cookie是会话级别的,浏览器关闭就会失效。

  • session 共享原理:

    当请求进来的时候,SessionRepositoryFilter 会先拦截到请求,将 request 和 response 对象转换成 SessionRepositoryRequestWrapper 和 SessionRepositoryResponseWrapper 。后续当第一次调用 request 的getSession方法时,会调用SessionRepositoryRequestWrapper 的getSession方法。这个方法是被重写过的,逻辑是先从 request 的属性中查找,如果找不到;再查找一个key值是"SESSION"的 Cookie,通过这个 Cookie 拿到 SessionId 去 Redis 中查找,如果查不到,就直接创建一个RedisSession 对象,同步到 Redis 中。

    说的简单点就是:拦截请求,将之前在服务器内存中进行 Session 创建销毁的动作,改成在 Redis 中创建。

Spring Session提供了一套创建和管理Servlet HTTPSession的方案,Spring Session提供了集群Session功能,默认采用外置的Redis来存储Session数据,以此来解决Session共享问题。

  1. 使用过滤器拦截请求

    
      
      <filter>
        <filter-name>springSessionRepositoryFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
      filter>
      <filter-mapping>
        <filter-name>springSessionRepositoryFilterfilter-name>
        <url-pattern>/*url-pattern>
      filter-mapping>
    
  2. 配置Redis

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                             http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            
            <property name="maxTotal" value="100">property>
            
            <property name="maxIdle" value="50">property>
            
            <property name="testOnBorrow" value="true"/>
            
            <property name="testOnReturn" value="true"/>
        bean>
        
        <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="hostName" value="192.168.175.100"/>
            <property name="port" value="6379"/>
            <property name="database" value="0"/>
            <property name="poolConfig" ref="jedisPoolConfig"/>
        bean>
        
        <bean id="redisHttpSessionConfiguration"
              class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
            <property name="maxInactiveIntervalInSeconds" value="600" />
        bean>
    beans>
    

你可能感兴趣的:(日更计划,spring,java,服务器)