Shiro 設置session超時時間

經過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);
相關文章
相關標籤/搜索