Shiro is a lightweight security framework that is easy to use and configure and is a good choice for small web projects.
Shiro's session can be set with a timeout value of 30 minutes by default.
By default, Shiro's SessionManager implementations default to a 30 minute session timeout. That is, if any Session created remains idle (unused, where its lastAccessedTime isn't updated)for 30 minutes or more, the Session is considered expired and will not be allowed to be used anymore.
This means that after 30 minutes of logon, the session times out and you need to log on again.
The timeout value can be customized in either of the following ways:
- Use the configuration file
Setting the Default Session Timeout in shiro.ini[main]...# 3,600,000 milliseconds = 1 hoursecurityManager.sessionManager.globalSessionTimeout = 3600000
- Through API: The session interface of Shiro has a setTimeout () method.
// After logging on, you can use the following method to obtain sessionsecurityutils. getsubject (). getsession (). setTimeout (30000 );
View Shiro API documentation,
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
Calling this method. Since: 0.2
The maximum time set, positive and negative. If it is a negative number, it never times out. When setting a negative number during development, you may encounter some problems:
SecurityUtils.getSubject().getSession().setTimeout(-1l);
After this call, the session is always thrown out of date and the cause cannot be found. Later, the source code is debugged and the time unit set here is Ms, But Shiro will convert the time: s, and the fractional part will be removed. In this way, the value is-1 ms, and 0 s after it is converted to S. It will expire immediately, so when we perform operations on this session, an exception is always thrown. The correct setting never times out should be:
// Timeout:-1000 ms never times out. securityutils. getsubject (). getsession (). setTimeout (-1000l );
This is probably because the API documentation is not detailed enough.