经过api:Shiro的Session接口有一个setTimeout()方法java
//登陆后,能够用以下方式取得session SecurityUtils.getSubject().getSession().setTimeout(30000);
查看Shiro的api文档,api
setTimeoutsession
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.2this
设置的最大时间,正负均可以,为负数时表示永不超时。开发过程当中,设置负数时,遇到点儿问题:调试
SecurityUtils.getSubject().getSession().setTimeout(-1l);
这样调用后,老是抛出session已通过时的异常,一直找不到缘由,后来调试源码才发现,这里设置的时间单位是:ms,可是Shiro会把这个时间转成:s,并且是会舍掉小数部分,这样我设置的是-1ms,转成s后就是0s,立刻就过时了,因此后面再对这个会话进行操做时,总会抛异常,正确的设置永不超时的方式应该是:code
// timeout:-1000ms 永不超时SecurityUtils.getSubject().getSession().setTimeout(-1000l);