Gracefully handle expired HttpSession in Spring WebFlow

(From the SpringSource Forum .)

When the HttpSession expires and the user resubmits the page to the stream, it is sent back to the beginning of the stream. All I want to add to this behavior is a post explaining why this happened. "You were inactive, so you were restarted ..."

What is the easiest / best way to do this?

+6
source share
2 answers

The default behavior, in FlowHandlerAdapter.defaultHandleException () , "tries to start a new thread execution with a completed or expired".

It seems like WebFlow's way to do this is to provide a FlowHandler using the handleException () method that checks for instanceof NoSuchFlowExecutionException , then do something like create a redirect URL or put something in a session area that can later be deleted after use .

Due to the way WebFlow uses redirects, I don’t think any other areas will allow this flag or message to be used later when a new stream view appears.

However, simply discovering a new session in Interceptor or even Filter would seem equally effective. This is what I ultimately did in my previous research on this issue, as described in the related forum topic. I was just hoping for something beautiful.

In addition, by the time the new stream begins, a new session identifier has already been created, so there is no way to initially detect this condition from the .xml stream.

Filter logic example:

 if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) { log.info("Expired Session ID: " + request.getRequestedSessionId()); response.sendRedirect("sessionExpired"); } else { chain.doFilter(request, response); } 

Reverse Interceptor:

 public class SessionExpiredInterceptor extends HandlerInterceptorAdapter { private String redirectLocation = "sessionExpired"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) { response.sendRedirect(redirectLocation); return false; } return true; } public String getRedirectLocation() { return redirectLocation; } public void setRedirectLocation(String redirectLocation) { this.redirectLocation = redirectLocation; } } 
+4
source

Step 1: FlowController has a handlerAdapter adapter. To configure session exceptions, you need to write your own adapter adapter and register it using the bean flow controller, as shown below:

 <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController"> . .<property name="flowHandlerAdapter" ref="customFlowHandlerAdapter"/> . </bean> <bean id="customFlowHandlerAdapter" class="gov.mo.courts.pbw.adapters.CustomFlowHandlerAdapter" p:flowExecutor-ref="flowExecutor"/> 

Step 2: CustomFlowHandlerAdapter In this class, override the defaultHandleException method. This is the method that the web stream calls in case of exceptions and reinitializes the session. note that a new session has already been created up to this point. Only the type of exception will show you that the previous session was disconnected.

 public class PbwFlowHandlerAdapter extends FlowHandlerAdapter{ protected void defaultHandleException(String flowId, FlowException e, HttpServletRequest request, HttpServletResponse response) throws IOException { if(e instanceof NoSuchFlowExecutionException){ if(e.getCause() instanceof NoSuchConversationException){ //"use newly created session object within request object to save your customized message." } } super.defaultHandleException(flowId, e, request, response); } 

The first page to view your application should display this message.

 <% if (session.getAttribute(YOUR_CUSTOM_MSG_KEY) != null) { %> <p class="errormessage"> <%=session.getAttribute(YOUR_CUSTOM_MSG_KEY)%> </p> <% //once the message has been shown, remove it from the session //as a new session has already been started at this time session.removeAttribute(YOUR_CUSTOM_MSG_KEY); } %> 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/911868/


All Articles