Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiro expired session handling

My standalone application uses Shiro for security management. I am encountering a problem with expired sessions. If a user session gets expired and when I try to log the user back in I get the following exception. Could anybody help?

org.apache.shiro.session.UnknownSessionException: There is no session with id [d32af383-5f26-463f-a2f0-58a0e82c7890]
 at org.apache.shiro.session.mgt.eis.AbstractSessionDAO.readSession(AbstractSessionDAO.java:170)
 at org.apache.shiro.session.mgt.DefaultSessionManager.retrieveSessionFromDataSource(DefaultSessionManager.java:236)
 at org.apache.shiro.session.mgt.DefaultSessionManager.retrieveSession(DefaultSessionManager.java:222)
 at org.apache.shiro.session.mgt.AbstractValidatingSessionManager.doGetSession(AbstractValidatingSessionManager.java:118)
 at org.apache.shiro.session.mgt.AbstractNativeSessionManager.lookupSession(AbstractNativeSessionManager.java:105)
 at org.apache.shiro.session.mgt.AbstractNativeSessionManager.lookupRequiredSession(AbstractNativeSessionManager.java:109)
 at org.apache.shiro.session.mgt.AbstractNativeSessionManager.stop(AbstractNativeSessionManager.java:238)
 at org.apache.shiro.session.mgt.DelegatingSession.stop(DelegatingSession.java:127)
 at org.apache.shiro.session.ProxiedSession.stop(ProxiedSession.java:107)
 at org.apache.shiro.subject.support.DelegatingSubject$StoppingAwareProxiedSession.stop(DelegatingSubject.java:419)
 at org.apache.shiro.session.ProxiedSession.stop(ProxiedSession.java:107)
 at org.apache.shiro.subject.support.DelegatingSubject$StoppingAwareProxiedSession.stop(DelegatingSubject.java:419)

I am using spring to configure shiro

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager"> 
    <property name="realm" ref="myRealm"/>
    <property name="sessionManager.globalSessionTimeout" value="3600000" />
</bean> 
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/> 
    <property name="arguments" ref="securityManager"/> 
</bean> 
like image 588
Amar Sosa Avatar asked Dec 12 '12 13:12

Amar Sosa


1 Answers

I'm facing the same issue while using a remote ejb for authentication.

As a workaround the first login attempt is in a try/catch block catching the UnknownSessionException. A Subject is then built from scratch for logging in the user again.

UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
    subject.login(token);
} catch (UnknownSessionException use) {
    subject = new Subject.Builder().buildSubject();
    subject.login(token);
    session = subject.getSession(true);
}
like image 126
zellus Avatar answered Nov 20 '22 19:11

zellus