Shiro complaining “There is no session with id xxx” with DefaultSecurityManager

前端 未结 3 1810
青春惊慌失措
青春惊慌失措 2021-02-19 22:47

I\'m using Apache Shiro 1.2.0 in a long-running application that reads messages from a queue and and takes action. The action taken requires a Shiro authenticated session, so I

相关标签:
3条回答
  • 2021-02-19 23:00

    Shiro is validating credentials against SecuritySubject, which is stored in Session. So, it's very likely your session expired after some time of inactivity. You can change expiration time in web.xml or you can use Shiro rememberMe function, but your client have to support cookies. After rememberMe function SecuritySubject will obtain different session and will return false against isAuthenticated, but isRemembered will return true.

    The session will never expired This will produce another problem, when your session will never expire. It will most likely get you out of memory, because your web container is most likely using memory session manager.

    <session-config>
        <session-timeout>-1</session-timeout>
    </session-config>
    

    Shiro rememberMe http://shiro.apache.org/java-authentication-guide.html

    //Example using most common scenario:
    //String username and password.  Acquire in
    //system-specific manner (HTTP request, GUI, etc)
    
    UsernamePasswordToken token =
     new UsernamePasswordToken( username, password );
    
    //”Remember Me” built-in, just do this:
    token.setRememberMe(true);
    
    0 讨论(0)
  • 2021-02-19 23:02

    We can disable the session storage in shiro.

    The org.apache.shiro.mgt.DefaultSessionStorageEvaluator class contains a flag called sessionStorageEnabled. We can make it false.

    I use the following in my spring application context for not using session storage.

    <bean id="defaultSessionStorageEvaluator" class="org.apache.shiro.mgt.DefaultSessionStorageEvaluator">
            <property name="sessionStorageEnabled" value="false" />
    

    <bean id="defaultSubjectDAO" class="org.apache.shiro.mgt.DefaultSubjectDAO">
            <property name="sessionStorageEvaluator" ref="defaultSessionStorageEvaluator" />
        </bean>
    
    0 讨论(0)
  • 2021-02-19 23:14

    I was getting this error and found that completely destroying any existing session before calling subject.login(credentials) fixed it.

    // Login the user
    private Subject loginUser()
    {
      ensureUserIsLoggedOut();
      Subject subject = SecurityUtils.getSubject();
      subject.login(credentials);
    }
    

    And the supporting routines are:

    // Logout the user fully before continuing.
    private void ensureUserIsLoggedOut()
    {
        try
        {
            // Get the user if one is logged in.
            Subject currentUser = SecurityUtils.getSubject();
            if (currentUser == null)
                return;
    
            // Log the user out and kill their session if possible.
            currentUser.logout();
            Session session = currentUser.getSession(false);
            if (session == null)
                return;
    
            session.stop();
        }
        catch (Exception e)
        {
            // Ignore all errors, as we're trying to silently 
            // log the user out.
        }
    }
    
    0 讨论(0)
提交回复
热议问题