You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Moksedul Alam <mo...@yahoo.com> on 2013/06/05 17:11:33 UTC

Shiro throws UnknownSessionException when logout / re-login a user with expired session

Hi,

I am having a problem when re-login a user whose session managed by 
Shiro has been expired. This issue (SHIRO-329) is supposed to be fixed 
in version 1.2.0, but still I am having this problem even in version 1.2.2.

Below is my test code snippet, which throws an UnknownSessionException.

     Factory<SecurityManager> factory = new 
IniSecurityManagerFactory("classpath:shiro.conf");
     SecurityUtils.setSecurityManager(factory.getInstance());

     UsernamePasswordToken token = new UsernamePasswordToken("user", 
"pass");

     Subject subject = SecurityUtils.getSubject();
     subject.login(token);
     subject.getSession().setTimeout(0);
     System.out.println("user is authenticated:: 
"+subject.getSession().getId());

     subject = SecurityUtils.getSubject();
     subject.logout();
     //subject.login(token);

UnknownSessionException is thrown both at the place of subject.logout() 
and the second login attempt (commented out). A quick debug showed me 
that the authentication happens correctly but after that when 
re-attaching some properties like host, session to SubjectContext tries 
to load the previous invalidated/expired session and the exception is 
thrown.

Is there any work around for this?

Thanks in advance.

Best Regards,
/Dewan

Re: Shiro throws UnknownSessionException when logout / re-login a user with expired session

Posted by Moksedul Alam <mo...@yahoo.com>.
Hi,

This occurs only when there is a session timeout and subsequent login 
logout for that user.

It seems that, on successful login/logout shiro creates new subject 
context and merge principles, etc. those tasks require a session for 
some reason and eventually try to load a session from current context 
which is already expired and removed from the datasource.

While trying I have had different stack traces each one leading to 
unknown session exception at some point. For example, for one case when 
login I have a stack trace like following

     	...
     	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:108)
     	at org.apache.shiro.session.mgt.AbstractNativeSessionManager.lookupRequiredSession(AbstractNativeSessionManager.java:112)
     	at org.apache.shiro.session.mgt.AbstractNativeSessionManager.getHost(AbstractNativeSessionManager.java:197)
     	at org.apache.shiro.session.mgt.DelegatingSession.getHost(DelegatingSession.java:111)
     	at org.apache.shiro.session.ProxiedSession.getHost(ProxiedSession.java:93)
     	at org.apache.shiro.session.ProxiedSession.getHost(ProxiedSession.java:93)
     	...
	at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:344)
	at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:183)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:283)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
	...

This indicates that, at some point when getting host it requires a 
session. Now if I set the host name in the auth token, then the trace is 
a bit different.

	...
	at org.apache.shiro.session.mgt.AbstractValidatingSessionManager.doGetSession(AbstractValidatingSessionManager.java:118)
	at org.apache.shiro.session.mgt.AbstractNativeSessionManager.lookupSession(AbstractNativeSessionManager.java:108)
	at org.apache.shiro.session.mgt.AbstractNativeSessionManager.lookupRequiredSession(AbstractNativeSessionManager.java:112)
	at org.apache.shiro.session.mgt.AbstractNativeSessionManager.getAttribute(AbstractNativeSessionManager.java:209)
	at org.apache.shiro.session.mgt.DelegatingSession.getAttribute(DelegatingSession.java:141)
	at org.apache.shiro.session.ProxiedSession.getAttribute(ProxiedSession.java:121)
	at org.apache.shiro.session.ProxiedSession.getAttribute(ProxiedSession.java:121)
	at org.apache.shiro.session.ProxiedSession.getAttribute(ProxiedSession.java:121)
	at org.apache.shiro.subject.support.DelegatingSubject.getRunAsPrincipalsStack(DelegatingSubject.java:469)
	at org.apache.shiro.subject.support.DelegatingSubject.isRunAs(DelegatingSubject.java:441)
	at org.apache.shiro.mgt.DefaultSubjectDAO.mergePrincipals(DefaultSubjectDAO.java:187)
	at org.apache.shiro.mgt.DefaultSubjectDAO.saveToSession(DefaultSubjectDAO.java:166)
	at org.apache.shiro.mgt.DefaultSubjectDAO.save(DefaultSubjectDAO.java:147)
	at org.apache.shiro.mgt.DefaultSecurityManager.save(DefaultSecurityManager.java:383)
	at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:350)
	at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:183)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:283)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
	...



On 6/5/13 8:07 PM, Paul Holding wrote:
> Hi Dewan
>
> Forgot to ask do you only get the UnknownSessionException error thrown when
> the session has already expired or does it occur whenever you are using
> Shiro's built in session management and then execute the logout() method?
>
> Also, whilst trying to troubleshoot this problem I set the logging level to
> FINEST and found that when Shiro executes the logout() method it appeares to
> have successfully stopped the session and then for some reason attempted to
> retrieve the session, at which point the error is thrown. I've uploaded the
> trace to pastebin  http://pastebin.com/UdmC5bvL
> <http://pastebin.com/UdmC5bvL>  . Would you be able to increase the logging
> level and see if you get a similar result please?
>
> Kind Regards
>
> Paul
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/Shiro-throws-UnknownSessionException-when-logout-re-login-a-user-with-expired-session-tp7578800p7578802.html
> Sent from the Shiro User mailing list archive at Nabble.com.


Re: Shiro throws UnknownSessionException when logout / re-login a user with expired session

Posted by Paul Holding <pa...@pholding.co.uk>.
Hi Dewan

Forgot to ask do you only get the UnknownSessionException error thrown when
the session has already expired or does it occur whenever you are using
Shiro's built in session management and then execute the logout() method?

Also, whilst trying to troubleshoot this problem I set the logging level to
FINEST and found that when Shiro executes the logout() method it appeares to
have successfully stopped the session and then for some reason attempted to
retrieve the session, at which point the error is thrown. I've uploaded the
trace to pastebin  http://pastebin.com/UdmC5bvL
<http://pastebin.com/UdmC5bvL>  . Would you be able to increase the logging
level and see if you get a similar result please?

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Shiro-throws-UnknownSessionException-when-logout-re-login-a-user-with-expired-session-tp7578800p7578802.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: Shiro throws UnknownSessionException when logout / re-login a user with expired session

Posted by Paul Holding <pa...@pholding.co.uk>.
Hi Dewan 

I've got a problem very similar to the one you're reporting and was just
about to start a similar new topic on the mailing list :) 

The problem I've got is that the UnknownSessionException is thrown when the
subject.logout() method is executed. For myself the issue only occurs when I
use Shiro's built in session management and if I comment out the relevent
lines in the shiro.ini file to allow the container to manage the sessions
then the problem doesn't occur and the logout works correctly.

Someone else has also reported a similar issue on the mailing list but it
doesn't appear the problem has been resolved. 
http://shiro-user.582556.n2.nabble.com/What-might-cause-an-UnknownSessionException-td7578179.html
<http://shiro-user.582556.n2.nabble.com/What-might-cause-an-UnknownSessionException-td7578179.html>  

It sounds like your application is not running within a container but I'd be
interested to see whether you find a fix for this issue.

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Shiro-throws-UnknownSessionException-when-logout-re-login-a-user-with-expired-session-tp7578800p7578801.html
Sent from the Shiro User mailing list archive at Nabble.com.