tomcat之session过期问题及Redis session管理配置
使用版本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. -- WatchedResourceWEB-INF/web.xml/WatchedResource !-- Uncomment this to disable session persistence across Tomcat restarts -- !-- Manager pathname / -- /Contextconf/web.xml以及工程中web.xml配置文件以工程web.xml的优先级最高。在工程配置session相关内容后该工程不受tomcat中的web.xml的配置影响。设置时间-1 :永不过期 30即30分钟过期。!-- You can set the default session timeout (in minutes) for all newly -- !--created sessions by modifying the value below. -- session-config session-timeout30/session-timeout /session-config- 二、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.jar2.在conf/content.xml 添加的Context中下面配置Valve classNamecom.radiadesign.catalina.session.RedisSessionHandlerValve / Manager className com.radiadesign.catalina.session.RedisSessionManager host127.0.0.1 database0 port 6379 passwordxxxx maxInactiveInterval3600 /我对content.xml里设置 maxInactiveInterval 配置无效一直很疑惑后从https://blog.csdn.net/jin5203344/article/details/52227810文章中找到答案。3.工程中的修改web.xml的session过期时间session out不可以配置为-1.不信可以试试哈会导致session存储不到Redis.session-config session-timeout7200/session-timeout /session-config4.验证成功方法a.访问该工程中的url连接session不变化b. 启动redis自身的客户端redis-cli -h 127.0.0.1 -p xxxkeys *会看到SESSIONIDget B1566A03631CB06D87EE471B6C2AB556得到SESSIONID的值。ttl B1566A03631CB06D87EE471B6C2AB556可以看到session的过期时间以秒为单位。

相关新闻