博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shiro 设置session超时时间
阅读量:6946 次
发布时间:2019-06-27

本文共 1343 字,大约阅读时间需要 4 分钟。

通过api:Shiro的Session接口有一个setTimeout()方法

//登录后,可以用如下方式取得sessionSecurityUtils.getSubject().getSession().setTimeout(30000);

查看Shiro的api文档,

setTimeout

void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionExceptionSets the time in milliseconds that the session may remain idle before expiring.A negative value means the session will never expire.A non-negative value (0 or greater) means the session

expiration will occur if idle for that length of time.*Note: if you are used to the HttpSession's getMaxInactiveInterval() method, the scale on this method is different: Shiro Sessions use millisecond values for timeout whereas HttpSession.getMaxInactiveInterval
uses seconds. Always use millisecond values with Shiro sessions.Parameters:maxIdleTimeInMillis - the time in milliseconds that the session may remain idle before expiring.Throws:InvalidSessionException - if the session has been stopped or expired prior to
calling this method.Since:0.2

 

设置的最大时间,正负都可以,为负数时表示永不超时。开发过程中,设置负数时,遇到点儿问题:

 
SecurityUtils.getSubject().getSession().setTimeout(-1l);
 

这样调用后,总是抛出session已经过时的异常,一直找不到原因,后来调试源码才发现,这里设置的时间单位是:ms,但是Shiro会把这个时间转成:s,而且是会舍掉小数部分,这样我设置的是-1ms,转成s后就是0s,马上就过期了,所以后面再对这个会话进行操作时,总会抛异常,正确的设置永不超时的方式应该是:

 

// timeout:-1000ms 永不超时 SecurityUtils.getSubject().getSession().setTimeout(-1000l);

转载于:https://www.cnblogs.com/yaomajor/p/7992791.html

你可能感兴趣的文章
python之configparse模块
查看>>
CentOS6.2编译安装MySQL5.5.25
查看>>
Nyoj 星际之门(一)(Cayley定理)
查看>>
词法分析程序
查看>>
前端基础之css
查看>>
网址收藏
查看>>
人的成长,注定是一场孤独的旅途 ...(360doc)
查看>>
死锁排查的小窍门 --使用jdk自带管理工具jstack
查看>>
安卓开发者必备的42个链接
查看>>
DeadLine
查看>>
2018-2019 Exp2 后门原理与实践
查看>>
bzoj5137 [Usaco2017 Dec]Standing Out from the Herd
查看>>
Mysql压缩包版zip的安装方法
查看>>
UWP 动画
查看>>
浅析设计模式(二)——工厂方法模式
查看>>
面试宝典-面试题1
查看>>
DAY1 linux 50条命令
查看>>
Eclipse设置Tab键为四个空格
查看>>
Windows漏洞利用技术概述
查看>>
多态与接口
查看>>