Shiro sets Session Timeout

Source: Internet
Author: User

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:

  1. Use the configuration file
    Setting the Default Session Timeout in shiro.ini[main]...# 3,600,000 milliseconds = 1 hoursecurityManager.sessionManager.globalSessionTimeout = 3600000

  2. 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.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.