Catch “dead” session in Spring webflow

被刻印的时光 ゝ 提交于 2019-11-29 16:50:10
Selwyn

With any of the proposed solutions below you should be able to handle the exceptions and perform logic on them.

Possible Solutions:

1. You can add a FlowExecutionListenerAdapter that will listen to all exceptions thrown and then you can filter by instanceof.

import org.springframework.webflow.execution.FlowExecutionListenerAdapter;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.webflow.engine.FlowAttributeMappingException;
import org.springframework.webflow.execution.ActionExecutionException;
import org.springframework.webflow.execution.FlowExecutionException;
import org.springframework.webflow.execution.RequestContext;

public class LoggingExceptionFlowExecutionListenerAdapter extends FlowExecutionListenerAdapter {

    @Override
    public void exceptionThrown(RequestContext context, FlowExecutionException exception) {
         if (exception instanceof HttpSessionRequiredException) {
                // do something
         } else if(exception instanceof FlowAttributeMappingException) {
                // do something else 
         }
    }
}

and you need to add it in your webflow executor config:

<bean id="loggingExceptionFlowExecutionListenerAdapter" class="my.package.LoggingExceptionFlowExecutionListenerAdapter"/>

<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
    <webflow:flow-execution-listeners>
       <webflow:listener ref="loggingExceptionFlowExecutionListenerAdapter"        
    </webflow:flow-execution-listeners>
</webflow:flow-executor>

2. Another solution is to implement your own FlowExecutionExceptionHandler. Although it will be a global exception handler The method

public void handle(FlowExecutionException exception,
            RequestControlContext context) { } 

will allow you to have access to the context which will allow you to filter on many different variables. See also, How implement the interface FlowExecutionExceptionHandler

3. Another possible solution if you are using Spring Security project I believe Spring security will handle the exception for you automatically if you have the follow config:

<security:http auto-config="true" use-expressions="true">
        <!-- rest of config omitted -->
        <security:session-management invalid-session-url="/login"/>
</security:http>

and if you want to catch the exception and perform logic on it see the following answer: Logout/Session timeout catching with spring security

I would recommend the 3rd solution as it is the least intrusive. In the 3rd solution's link there are several really elegent solutions to handling sessions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!