tomcat之session过期问题及Redis session管理配置
阅读原文时间:2021年04月20日阅读:1

使用版本tomat 7

一、Tomcat相关影响session管理的配置文件
1 . conf/content.xml,默认配置中Manager 注释不打开情况下,重启后后保留session不失效。
Manager 打开情况下,重启后会丢失session

<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts --> 
    <!--
    <Manager pathname="" />
    -->
</Context>
  1. conf/web.xml以及工程中web.xml配置文件,以工程web.xml的优先级最高。在工程配置session相关内容后,该工程不受tomcat中的web.xml的配置影响。
    设置时间:-1 :永不过期 30:即30分钟过期。



    30

- 二、tomcat配置redis实现session共享(tomcat7)

1.将下面jar包添加到tomcat的lib文件夹中。
commons-pool-1.6.jar、commons-pool2-2.2.jar、jedis-2.2.0.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar
2.在conf/content.xml 添加的Context中下面配置

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className = "com.radiadesign.catalina.session.RedisSessionManager" host="127.0.0.1"  
database="0"  
port=" 6379 "
 password="xxxx"
      maxInactiveInterval="3600" />

我对content.xml里设置 maxInactiveInterval 配置无效,一直很疑惑,后从https://blog.csdn.net/jin5203344/article/details/52227810
文章中找到答案。
3.工程中的修改web.xml的session过期时间:session out不可以配置为-1.(不信,可以试试哈),会导致session存储不到Redis.

<session-config>
        <session-timeout>7200</session-timeout>
 </session-config>

4.验证成功方法:
a.访问该工程中的url,连接,session不变化
b. 启动redis自身的客户端:redis-cli -h 127.0.0.1 -p xxx
keys *,会看到SESSIONID
get B1566A03631CB06D87EE471B6C2AB556 ,得到SESSIONID的值。
ttl B1566A03631CB06D87EE471B6C2AB556 可以看到session的过期时间,以秒为单位。