You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Paul Holding <pa...@pholding.co.uk> on 2013/06/06 01:26:11 UTC

UnknownSessionException when calling logout() method using Shiro's built in session management

I need to allow user in my web based application to click on a logout button
to securely end their session. The logout button executes the
"SecurityUtils.getSubject().logout()" method and then redirects the user
back to the login page. When Shiro uses the default container provided
session management everything works as expected, however if I enable Shiro's
built in session management, then an UnknownSessionException is thrown
whenever the "SecurityUtils.getSubject().logout()" method is executed. Note
that this occurs before the next line of code to redirect the user back to
the login page is executed.

Whilst trying to troubleshoot this problem I set the logging level to FINEST
and found that when Shiro executes the logout() method it appears 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.

The contents of the Shiro.ini file is as follows:
[main]
user = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter
user.loginUrl = /login.xhtml

sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager

securityManager.sessionManager = $sessionManager

[users]
<removed>

[urls]
/login.xhtml = user
/app/** = user

I have reproduced this issue on both WebSphere Application Server 8.5 and
GlassFish 3.1.2.2 using Shiro 1.2.2

I was about to start this thread earlier today but I saw an almost identical
thread titled " Shiro throws UnknownSessionException when logout / re-login
a user with expired session
<http://shiro-user.582556.n2.nabble.com/Shiro-throws-UnknownSessionException-when-logout-re-login-a-user-with-expired-session-td7578800.html> 
" , however it appears that the specific issue reported there only occurs
when the session has expired, although in my case the session hasn't
expired. Rather than continue to highjack someone else’s thread I thought it
would be better to start a new one. I did also find a thread titled " What
might cause an UnknownSessionException
<http://shiro-user.582556.n2.nabble.com/What-might-cause-an-UnknownSessionException-td7578179.html> 
" which looked promising but I would really prefer to stop the session
correctly rather than using a work around.

Is using "SecurityUtils.getSubject().logout()" the correct way to allow a
user to end their own session when using Shiro's built in session management
and if so what can I do to stop the exception being thrown without resorting
to workarounds such as removing the session attributed? I have tried
wrapping the method in a try/catch statement, however it seems the exception
is being caught by something else in the call stack.




--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

Posted by Paul Holding <pa...@pholding.co.uk>.
Paul Holding wrote
> javax.servlet.ServletException: /app/index.xhtml at line 39 and column 82
> action="#{logout.submit}": java.lang.IllegalStateException:
> org.apache.shiro.session.UnknownSessionException: There is no session with
> id [37de04fc-8ec2-48ea-8c14-aeb8053ebd7b]

Forgot to mention that this error message was captured a few minutes ago
whereas the trace uploaded to pastebin was captured several days ago so the
session IDs will be different between the two.



--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578808.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

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


Les Hazlewood-2 wrote
> It appears that MyFaces is trying to use the Session after it has been 
> closed - the renderView method calls AbstractThreadSafeAttributeMap 
> which in turn tries to use the Session.  It is almost as if an HTTP 
> redirect is _not_ happening.

Unfortunately I've got myself in to a bit of a mess whilst playing with the
code in my logout backing bean and taking the traces to troubleshoot the
problem. It seems the trace I took yesterday was done whilst the redirect
line of code was commented out. Nevertheless even with the redirect
reinstated the exception "java.lang.IllegalStateException:
org.apache.shiro.session.UnknownSessionException" is still thrown, and the
only difference is the section "Error Rendering View[/app/index.xhtml]" in
the trace doesn't happen and instead goes straight to "An exception was
thrown by one of the service methods of the servlet [Faces Servlet] in
application [Test-EAR]. Exception created :
[java.lang.IllegalStateException:
org.apache.shiro.session.UnknownSessionException". Sorry for any confusion
caused by this.


Les Hazlewood-2 wrote
> Does Faces.redirect actually execute a 
> 302 redirect?  Or does it do something JSF-ish instead? 

Faces.redirect is a method provided by a library called Omnifaces. The
Faces.redirect method is wrapper around the JSF redirect method to reduce
the amount of boilerplate code in the backing bean. The JavaDoc for this
method says it "Sends a temporary (302) redirect to the given URL"


Les Hazlewood-2 wrote
> As to your question about Shiro's behavior vs your Servlet container's 
> behavior: Shiro's web support attempts to acquire a request's 
> referenced session at the beginning of a request chain (i.e. top of 
> the filter stack).  If the session is expired or non-existent, then 
> the SessionException is ignored, with the expectation that a new 
> Session will be created later in the request if it is needed.  This 
> effectively eliminates downstream code from worrying about 
> SessionExceptions. 
> 
> However, Shiro currently does _not_ swallow these exceptions if the 
> Session is terminated and that same session is attempted to be used 
> again.  Maybe a less intrusive behavior would be to only throw the 
> SessionException if the response hasn't yet been committed (because if 
> it had been committed, there wouldn't be a way to set the session ID 
> cookie, and the session couldn't be reliably referenced again, 
> indicating an error scenario). 
> 
> But I wonder if this proposed behavior might be less ideal than 
> Shiro's current behavior:  clearly you have logged out the user, 
> indicating the user is done using the application.  Doesn't it seem 
> odd to allow a new session to be created and populated with data if 
> that session will (probably) never be used again?  Why should 
> infrastructure (MyFaces) be allowed to create new sessions when you've 
> indicated that the user is done using the app? 
> 
> Current servlet containers that allow this behavior allow for 
> potentially very inefficient use of resources (many unused sessions 
> could be created, eating up session storage memory and/or disk). 
> 
> I'm curious as to your thoughts and what you'd like to see though! 

My initial thoughts are that if the session has been stopped then any
subsequent SessionException should be swallowed as I've decided that session
is no longer required and subsequently the user will be redirected to the
login page where they will acquire a new session. However on further
reflection this does seem like it would just be treating the symptom rather
than the underlying cause. Maybe a compromise would a parameter which
controls whether these exceptions are swallowed. I agree that it seems odd
to allow MyFaces to continue using the session once the user has indicated
they have finished using the app. Unfortunately I don't have a sufficiently
detailed knowledge of MyFaces to understand what's happening under the
covers.

On one hand I think that that if the exception could be swallowed by Shiro
it would solve this problem, but on the other hand I fear that the issues
you highlight around inefficient use of resources could create a bigger
problem in the future.

Assuming that the SessionException were to be swallowed, could it be flagged
in some way to be cleaned up by the session validation mechanism?


Les Hazlewood-2 wrote
> I do have a request that would help me greatly in trying to trace 
> things further though: would you be able to add the current thread ID 
> in the log message output?  It is hard to see if this stuff is 
> happening in a single thread of execution or across two requests (i.e. 
> It is hard to see with the log output if: 1) all of this happens in a 
> single thread or 2) the first request calls logout and redirects, and 
> the second request attempts to use the stale session ID. 

I've captured four traces which include the thread in the output.
1.  Shows the UnknownSessionException with no redirect (i.e Faces.redirect
method is commented out) <http://pastebin.com/U34qy7qD>  
2.  Shows the UnknownSessionException with the redirect to the login page
<http://pastebin.com/scWqrpAJ>  
3.  Shows the normal flow of events when using container managed session
with no redirect <http://pastebin.com/rnrCch4J>  
4.  Shows the normal flow of events when using container managed session
with the redirect to the login page. <http://pastebin.com/GT7tJPuH>  

Once again thank you for taking the time to look at this problem and sorry
for any confusion caused by me commenting out the redirect.

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578830.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

Posted by Les Hazlewood <lh...@apache.org>.
Fascinating - this is what I see:

[08/06/13 09:43:03:762 BST] 00000186 SimpleCookie  1
org.apache.shiro.web.servlet.SimpleCookie addCookieHeader Added
HttpServletResponse Cookie [JSESSIONID=deleteMe; Path=/Test-WAR;
Max-Age=0; Expires=Fri, 07-Jun-2013 08:43:03 GMT]
[08/06/13 09:43:03:762 BST] 00000186 SimpleCookie  3
org.apache.shiro.web.servlet.SimpleCookie removeFrom Removed
'JSESSIONID' cookie by setting maxAge=0
[08/06/13 09:43:03:793 BST] 00000186 AbstractValid 3
org.apache.shiro.session.mgt.AbstractValidatingSessionManager
doGetSession Attempting to retrieve session with key
org.apache.shiro.web.session.mgt.WebSessionKey@4f690448
[08/06/13 09:43:03:793 BST] 00000186 FaceletViewDe E   Error Rendering
View[/app/index.xhtml]
                                 java.lang.IllegalStateException:

If these log statements are a result of the same thread, then the
FaceletView (MyFaces) is trying to acquire a session after the Session
has been terminated.

Then, in the stack trace that follows:

        at org.apache.shiro.web.servlet.ShiroHttpSession.getAttribute(ShiroHttpSession.java:133)
        at org.apache.myfaces.context.servlet.SessionMap.getAttribute(SessionMap.java:50)
        at org.apache.myfaces.util.AbstractThreadSafeAttributeMap.put(AbstractThreadSafeAttributeMap.java:108)
        at org.apache.myfaces.util.AbstractThreadSafeAttributeMap.put(AbstractThreadSafeAttributeMap.java:38)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.getResponseEncoding(FaceletViewDeclarationLanguage.java:1699)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.createResponseWriter(FaceletViewDeclarationLanguage.java:1594)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1297)
        at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59)
        at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
        at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)

It appears that MyFaces is trying to use the Session after it has been
closed - the renderView method calls AbstractThreadSafeAttributeMap
which in turn tries to use the Session.  It is almost as if an HTTP
redirect is _not_ happening.  Does Faces.redirect actually execute a
302 redirect?  Or does it do something JSF-ish instead?

As to your question about Shiro's behavior vs your Servlet container's
behavior: Shiro's web support attempts to acquire a request's
referenced session at the beginning of a request chain (i.e. top of
the filter stack).  If the session is expired or non-existent, then
the SessionException is ignored, with the expectation that a new
Session will be created later in the request if it is needed.  This
effectively eliminates downstream code from worrying about
SessionExceptions.

However, Shiro currently does _not_ swallow these exceptions if the
Session is terminated and that same session is attempted to be used
again.  Maybe a less intrusive behavior would be to only throw the
SessionException if the response hasn't yet been committed (because if
it had been committed, there wouldn't be a way to set the session ID
cookie, and the session couldn't be reliably referenced again,
indicating an error scenario).

But I wonder if this proposed behavior might be less ideal than
Shiro's current behavior:  clearly you have logged out the user,
indicating the user is done using the application.  Doesn't it seem
odd to allow a new session to be created and populated with data if
that session will (probably) never be used again?  Why should
infrastructure (MyFaces) be allowed to create new sessions when you've
indicated that the user is done using the app?

Current servlet containers that allow this behavior allow for
potentially very inefficient use of resources (many unused sessions
could be created, eating up session storage memory and/or disk).

I'm curious as to your thoughts and what you'd like to see though!

I do have a request that would help me greatly in trying to trace
things further though: would you be able to add the current thread ID
in the log message output?  It is hard to see if this stuff is
happening in a single thread of execution or across two requests (i.e.
It is hard to see with the log output if: 1) all of this happens in a
single thread or 2) the first request calls logout and redirects, and
the second request attempts to use the stale session ID.

I hope that helps clarify what I think is going on - I look forward to
your feedback!

Cheers,

--
Les Hazlewood | @lhazlewood
CTO, Stormpath | http://stormpath.com | @goStormpath | 888.391.5282

On Sat, Jun 8, 2013 at 9:09 AM, Paul Holding <pa...@pholding.co.uk> wrote:
> Hi Les
>
>
> Les Hazlewood-2 wrote
>> It is not possible for an exception to propagate past a catch clause
>> that you have defined without you detecting it - this means something
>> else (separate from your logout() call) is attempting to use a session
>> without you being aware of it.
>
> I've now stepped through most of the code being executed from the
> SecurityUtils.getSubject().logout() method onwards and it indeed seems that
> something else is going on. The submit method in the logout backing bean
> (shown below) completes successfully, so it seems that JSF is doing
> something after the logoff button has been presses and the method in the
> backing bean executed which is trying use the session.
>
>     public String submit() {
>     try {
>     System.out.println("****Logging out****");
>     SecurityUtils.getSubject().logout();
>     } catch (IllegalStateException e) {
>     System.out.println("Do nothing");
>     } catch (UnknownSessionException e) {
>     System.out.println("Do nothing");
>     }
>         Faces.redirect("login.xhtml");
>     return null;
>     }
>
>
> Les Hazlewood-2 wrote
>> I've seen this happen a few times in web apps, usually due to one of 2
>> cases:
>> the web request.
>>
>> 2) subject.logout() is called after the http response has already been
>> committed (body content is being rendered).  Because of the way
>> cookies work, it is not possible to delete the session id cookie in
>> this case, so subsequent requests will have an invalid session id
>> cookie and cause this exception.
>
>
> Having looked at all the output generated in the server log, I've spotted
> the following two entries that occur after the
> "java.lang.IllegalStateException:
> org.apache.shiro.session.UnknownSessionException" exception is thrown which
> I think might be relevant.
>
> com.ibm.ws.webcontainer.srt.SRTServletResponse setStatus WARNING: Cannot set
> status. Response already committed.
> com.ibm.ws.webcontainer.srt.SRTServletResponse addHeader SRVE8094W: WARNING:
> Cannot set header. Response already committed.
>
> Note that the above log entries only occur when using Shiro managed
> sessions.
>
> What I really don't understand is why these issues does not occur when I
> allow the container to manage the session (i.e. remove sessionManager =
> org.apache.shiro.web.session.mgt.DefaultWebSessionManager from the shiro.ini
> file). I've been able to reproduce this problem on both WebSphere and
> GlassFish so it would seem unlikely that it is the application server
> causing the problem although I know they both use Tomcat for the servlet
> container.
>
> I'm really struggling to understand what's going on here and why it works
> with container managed sessions but not with Shiro managed sessions.
>
> Kind Regards
>
> Paul
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578827.html
> Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

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


Les Hazlewood-2 wrote
> It is not possible for an exception to propagate past a catch clause
> that you have defined without you detecting it - this means something
> else (separate from your logout() call) is attempting to use a session
> without you being aware of it.

I've now stepped through most of the code being executed from the
SecurityUtils.getSubject().logout() method onwards and it indeed seems that
something else is going on. The submit method in the logout backing bean
(shown below) completes successfully, so it seems that JSF is doing
something after the logoff button has been presses and the method in the
backing bean executed which is trying use the session.

    public String submit() { 
    try { 
    System.out.println("****Logging out****"); 
    SecurityUtils.getSubject().logout(); 
    } catch (IllegalStateException e) { 
    System.out.println("Do nothing"); 
    } catch (UnknownSessionException e) { 
    System.out.println("Do nothing"); 
    } 
        Faces.redirect("login.xhtml"); 
    return null; 
    } 


Les Hazlewood-2 wrote
> I've seen this happen a few times in web apps, usually due to one of 2
> cases: 
> the web request. 
> 
> 2) subject.logout() is called after the http response has already been 
> committed (body content is being rendered).  Because of the way 
> cookies work, it is not possible to delete the session id cookie in 
> this case, so subsequent requests will have an invalid session id 
> cookie and cause this exception. 


Having looked at all the output generated in the server log, I've spotted
the following two entries that occur after the
"java.lang.IllegalStateException:
org.apache.shiro.session.UnknownSessionException" exception is thrown which
I think might be relevant.

com.ibm.ws.webcontainer.srt.SRTServletResponse setStatus WARNING: Cannot set
status. Response already committed.
com.ibm.ws.webcontainer.srt.SRTServletResponse addHeader SRVE8094W: WARNING:
Cannot set header. Response already committed.

Note that the above log entries only occur when using Shiro managed
sessions.

What I really don't understand is why these issues does not occur when I
allow the container to manage the session (i.e. remove sessionManager =
org.apache.shiro.web.session.mgt.DefaultWebSessionManager from the shiro.ini
file). I've been able to reproduce this problem on both WebSphere and
GlassFish so it would seem unlikely that it is the application server
causing the problem although I know they both use Tomcat for the servlet
container.

I'm really struggling to understand what's going on here and why it works
with container managed sessions but not with Shiro managed sessions.

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578827.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

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

Once again, thanks for taking the time to look at this problem.


Les Hazlewood-2 wrote
> It is strongly recommended in web apps that a logout call should do 
> nothing but call subject.logout() and then immediately redirect to a 
> new view. 

I've removed as much code as possible from the method in the backing bean
which is executed when the user clicks the logout button. The code is as
follows:

    public String submit() {
    	try {
    		System.out.println("****Logging out****");
    		SecurityUtils.getSubject().logout();
    	} catch (IllegalStateException e) {
    		System.out.println("Do nothing");
    	} catch (UnknownSessionException e) {
    		System.out.println("Do nothing");
    	}
	Faces.redirect("login.xhtml");
    	return null;
    }

I've even tried commenting out the redirect so that clicking on the logout
button should just end the session and nothing more, but the error is still
thrown and uncaught by the the above catch block.


Les Hazlewood-2 wrote
> I'd connect a debugger and put a breakpoint on
> DelegatingSubject.getSession() to see what in the call stack is
> attempting to get a session after you've explicitly called logout.

I've set a breakpoint on DelegatingSubject.getSession() and
DelegatingSubject.getSession(boolean). The DelegatingSubject.getSession()
method was not called, but the DelegatingSubject.getSession(boolean) method
was called four times after the user clicked on the logout button and in all
four cases the Logout.submit() method which in the backing bean method is in
the call stack.

The first time the getSession(boolean) method is called, the first 5 lines
of the call stack are:
WebDelegatingSubject(DelegatingSubject).getSession(boolean) line: 316	
WebDelegatingSubject(DelegatingSubject).clearRunAsIdentities() line: 475	
WebDelegatingSubject(DelegatingSubject).clearRunAsIdentitiesInternal() line:
353	
WebDelegatingSubject(DelegatingSubject).logout() line: 362	
Logout.submit() line: 18	

The second time the getSession(boolean) method is called, the first 6 lines
of the call stack are:
WebDelegatingSubject(DelegatingSubject).getSession(boolean) line: 316	
WebDelegatingSubject(DelegatingSubject).getRunAsPrincipalsStack() line: 467	
WebDelegatingSubject(DelegatingSubject).getPrincipals() line: 153	
DefaultWebSecurityManager(DefaultSecurityManager).logout(Subject) line: 547	
WebDelegatingSubject(DelegatingSubject).logout() line: 363	
Logout.submit() line: 18	

The third time the getSession(boolean) method is called, the first 7 lines
of the call stack are:
WebDelegatingSubject(DelegatingSubject).getSession(boolean) line: 316	
DefaultSubjectDAO.removeFromSession(Subject) line: 268	
DefaultSubjectDAO.delete(Subject) line: 281	
DefaultWebSecurityManager(DefaultSecurityManager).delete(Subject) line: 397	
DefaultWebSecurityManager(DefaultSecurityManager).logout(Subject) line: 559	
WebDelegatingSubject(DelegatingSubject).logout() line: 363	
Logout.submit() line: 18	

The fourth time the getSession(boolean) method is called, the first 5 lines
of the call stack are:
WebDelegatingSubject(DelegatingSubject).getSession(boolean) line: 316	
DefaultWebSecurityManager(DefaultSecurityManager).stopSession(Subject) line:
579	
DefaultWebSecurityManager(DefaultSecurityManager).logout(Subject) line: 567	
WebDelegatingSubject(DelegatingSubject).logout() line: 363	
Logout.submit() line: 18

I've uploaded a new trace with org.apache.shiro set to FINEST to pastebin
here  http://pastebin.com/tZwEhpWs <http://pastebin.com/tZwEhpWs>   and I've
uploaded the full call stack with the times cross references to the
getSession(boolean) calls in the trace to pastebin here 
http://pastebin.com/4eUr88yR <http://pastebin.com/4eUr88yR>  

Would you be able to take a look at the trace and call stack to see whether
this sheds any light on what might be causing the problem please?

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578826.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

Posted by Les Hazlewood <lh...@apache.org>.
Hi Paul,

It is not possible for an exception to propagate past a catch clause
that you have defined without you detecting it - this means something
else (separate from your logout() call) is attempting to use a session
without you being aware of it.

I've seen this happen a few times in web apps, usually due to one of 2 cases:

1) A web page (JSP or JSF) page attempts to use request.getSession()
without you knowing about it.  If this happens after subject.logout()
is called, you will see an error because the session ID cookie is
still present in the web request.

2) subject.logout() is called after the http response has already been
committed (body content is being rendered).  Because of the way
cookies work, it is not possible to delete the session id cookie in
this case, so subsequent requests will have an invalid session id
cookie and cause this exception.

It is strongly recommended in web apps that a logout call should do
nothing but call subject.logout() and then immediately redirect to a
new view.

By looking at your log, it appears something in the same thread is
calling subject.getSession() _after_ subject.logout():

[02/06/13 20:38:32:163 BST] 000032e7 DefaultSecuri 1
org.apache.shiro.mgt.DefaultSecurityManager logout Logging out subject
with primary principal admin
[02/06/13 20:38:32:178 BST] 000032e7 CachingRealm  3
org.apache.shiro.realm.CachingRealm clearCache Cleared cache entries
for account with principals [admin]
[02/06/13 20:38:32:178 BST] 000032e7 DelegatingSub 3
org.apache.shiro.subject.support.DelegatingSubject getSession
attempting to get session; create = false; session is null = false;
session has id = true

I'd connect a debugger and put a breakpoint on
DelegatingSubject.getSession() to see what in the call stack is
attempting to get a session after you've explicitly called logout.

HTH,
--
Les Hazlewood | @lhazlewood
CTO, Stormpath | http://stormpath.com | @goStormpath | 888.391.5282


On Thu, Jun 6, 2013 at 10:23 AM, Paul Holding <pa...@pholding.co.uk> wrote:
> Les Hazlewood-2 wrote
>> A quick workaround is to just catch and ignore the
>> exception and then issue the redirect.
>
> Hi Les
>
> Thanks for the response. I've tried wrapping the
> SecurityUtils.getSubject().logout() method in a try/catch but the exception
> isn't being caught by this catch block. Instead the error seems to be caught
> by the faces servlet. I've tried catching both
> java.lang.IllegalStateException and
> org.apache.shiro.session.UnknownSessionException but neither actually get
> caught.
>
> The error message returned by the faces servlet is as follows
>
> javax.servlet.ServletException: /app/index.xhtml at line 39 and column 82
> action="#{logout.submit}": java.lang.IllegalStateException:
> org.apache.shiro.session.UnknownSessionException: There is no session with
> id [37de04fc-8ec2-48ea-8c14-aeb8053ebd7b]
>
> Any ideas?
>
> Kind Regards
>
> Paul
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578807.html
> Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

Posted by Paul Holding <pa...@pholding.co.uk>.
Les Hazlewood-2 wrote
> A quick workaround is to just catch and ignore the
> exception and then issue the redirect.

Hi Les

Thanks for the response. I've tried wrapping the
SecurityUtils.getSubject().logout() method in a try/catch but the exception
isn't being caught by this catch block. Instead the error seems to be caught
by the faces servlet. I've tried catching both
java.lang.IllegalStateException and
org.apache.shiro.session.UnknownSessionException but neither actually get
caught.

The error message returned by the faces servlet is as follows

javax.servlet.ServletException: /app/index.xhtml at line 39 and column 82
action="#{logout.submit}": java.lang.IllegalStateException:
org.apache.shiro.session.UnknownSessionException: There is no session with
id [37de04fc-8ec2-48ea-8c14-aeb8053ebd7b]

Any ideas?

Kind Regards

Paul



--
View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804p7578807.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: UnknownSessionException when calling logout() method using Shiro's built in session management

Posted by Les Hazlewood <lh...@apache.org>.
Hi Paul - this is odd behavior.  We're using Shiro native session
management and redirect the user to the login page immediately after
calling subject.logout() as well, and we don't see these problems.

I'll take a peek at the paste as soon as I have time and let you know
what I find.  A quick workaround is to just catch and ignore the
exception and then issue the redirect.

HTH,
--
Les Hazlewood | @lhazlewood
CTO, Stormpath | http://stormpath.com | @goStormpath | 888.391.5282


On Wed, Jun 5, 2013 at 4:26 PM, Paul Holding <pa...@pholding.co.uk> wrote:
> I need to allow user in my web based application to click on a logout button
> to securely end their session. The logout button executes the
> "SecurityUtils.getSubject().logout()" method and then redirects the user
> back to the login page. When Shiro uses the default container provided
> session management everything works as expected, however if I enable Shiro's
> built in session management, then an UnknownSessionException is thrown
> whenever the "SecurityUtils.getSubject().logout()" method is executed. Note
> that this occurs before the next line of code to redirect the user back to
> the login page is executed.
>
> Whilst trying to troubleshoot this problem I set the logging level to FINEST
> and found that when Shiro executes the logout() method it appears 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.
>
> The contents of the Shiro.ini file is as follows:
> [main]
> user = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter
> user.loginUrl = /login.xhtml
>
> sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
>
> securityManager.sessionManager = $sessionManager
>
> [users]
> <removed>
>
> [urls]
> /login.xhtml = user
> /app/** = user
>
> I have reproduced this issue on both WebSphere Application Server 8.5 and
> GlassFish 3.1.2.2 using Shiro 1.2.2
>
> I was about to start this thread earlier today but I saw an almost identical
> thread titled " Shiro throws UnknownSessionException when logout / re-login
> a user with expired session
> <http://shiro-user.582556.n2.nabble.com/Shiro-throws-UnknownSessionException-when-logout-re-login-a-user-with-expired-session-td7578800.html>
> " , however it appears that the specific issue reported there only occurs
> when the session has expired, although in my case the session hasn't
> expired. Rather than continue to highjack someone else’s thread I thought it
> would be better to start a new one. I did also find a thread titled " What
> might cause an UnknownSessionException
> <http://shiro-user.582556.n2.nabble.com/What-might-cause-an-UnknownSessionException-td7578179.html>
> " which looked promising but I would really prefer to stop the session
> correctly rather than using a work around.
>
> Is using "SecurityUtils.getSubject().logout()" the correct way to allow a
> user to end their own session when using Shiro's built in session management
> and if so what can I do to stop the exception being thrown without resorting
> to workarounds such as removing the session attributed? I have tried
> wrapping the method in a try/catch statement, however it seems the exception
> is being caught by something else in the call stack.
>
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/UnknownSessionException-when-calling-logout-method-using-Shiro-s-built-in-session-management-tp7578804.html
> Sent from the Shiro User mailing list archive at Nabble.com.