Monday, September 16, 2013

Logging out with Apache Shiro and Spring Web Flow

In this article I will attempt to save others the frustration of figuring out how to log out with Apache Shiro security in a Spring Web Flow app. The Shiro site has a nice link on Spring integration and from incorporating that and finding the Subject.logout() method, only to use it resulting in a Spring Web Flow error about being unable to serialize a non-existent session (since you just killed it):
[INFO] Caused by: org.springframework.webflow.execution.repository.NoSuchFlowExecutionException: No flow execution could be found with key 'e1s2' -- perhaps this executing flow has ended or expired? This could happen if your users are relying on browser history (typically via the back button) that references ended flows.
[INFO]  at
[INFO]  at org.springframework.webflow.execution.repository.impl.DefaultFlowExecutionRepository.updateFlowExecutionSnapshot(
[INFO]  at org.springframework.webflow.engine.impl.FlowExecutionImpl.updateCurrentFlowExecutionSnapshot(
[INFO]  at org.springframework.webflow.engine.impl.RequestControlContextImpl.updateCurrentFlowExecutionSnapshot(
[INFO]  at org.springframework.webflow.engine.ViewState.updateHistory(
[INFO]  at org.springframework.webflow.engine.ViewState.exit(
[INFO]  at org.springframework.webflow.engine.Transition.execute(
[INFO]  at org.springframework.webflow.engine.impl.FlowExecutionImpl.execute(
[INFO]  at org.springframework.webflow.engine.impl.RequestControlContextImpl.execute(
[INFO]  at org.springframework.webflow.engine.TransitionableState.handleEvent(
[INFO]  at org.springframework.webflow.engine.Flow.handleEvent(
[INFO]  at org.springframework.webflow.engine.impl.FlowExecutionImpl.handleEvent(
[INFO]  at org.springframework.webflow.engine.impl.RequestControlContextImpl.handleEvent(
[INFO]  at org.springframework.webflow.engine.ViewState.handleEvent(
[INFO]  at org.springframework.webflow.engine.ViewState.resume(
[INFO]  at org.springframework.webflow.engine.Flow.resume(
[INFO]  at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(
[INFO]  ... 54 more
[INFO] Caused by: org.springframework.webflow.conversation.NoSuchConversationException: No conversation could be found with id '1' -- perhaps this conversation has ended? 
[INFO]  at org.springframework.webflow.conversation.impl.ConversationContainer.getConversation(
[INFO]  at org.springframework.webflow.conversation.impl.SessionBindingConversationManager.getConversation(
[INFO]  at
[INFO]  at
[INFO]  ... 70 more
From digging deeper into the documentation you can find a log out filter for Shiro, but using this instead of Subject.logout() in an action method (i.e. the action attribute of a button or link, not an action-state) will still give the same error. Lastly, you can find a blog post by BalusC (under Programmatic Logout) that describes how to tell JSF that you just logged out via Omnifaces in the action but that doesn't quite do the trick either. However, calling Subject.logout(), the code BalusC describes and then returning NULL from the action does it. Apparently the log out code doesn't stop SWF from trying to resume the flow, but returning null does. To summarize, the following will let you log out of Apache Shiro and Spring Web Flow when bound to the action of something like an h:button:
 public String logoutAction() throws IOException {
  return null;