抓住“死”在Spring Webflow中进行会话 [英] Catch "dead" session in Spring webflow

查看:375
本文介绍了抓住“死”在Spring Webflow中进行会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抓住当我松开会话时抛出的异常,不仅因为会话超时(例如报告)。
此外,我希望这个处理程序将只处理该特定的异常,并且不会是全局异常处理程序或任何这样的事情。
基本上,我想抓住异常 org.springframework.web.HttpSessionRequiredException

I want to catch the exception thrown when I loose my session, not only because a session timeout (for reports for example). Also I want that this handler will handle only that specific exception and will not be a global exceptions handler or any thing like this. Basically, I want to catch exception org.springframework.web.HttpSessionRequiredException.

推荐答案

使用下面提到的任何解决方案,您应该能够处理异常并对其执行逻辑。

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

可能的解决方案:

1。可以添加一个FlowExecutionListenerAdapter,它将侦听抛出的所有异常,然后可以通过instanceof进行过滤。

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 
         }
    }
}

,您需要将其添加到您的Webflow执行器配置中:

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。另一个解决方案是实现自己的FlowExecutionExceptionHandler。虽然它将是一个全局异常处理程序方法

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) { } 

将允许您访问上下文,允许您过滤许多不同的变量。另见如何实现界面FlowExecutionExceptionHandler

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。如果您使用Spring Security项目,另一个可能的解决方案是,如果您有以下配置,Spring Security将自动处理该异常:

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.

这篇关于抓住“死”在Spring Webflow中进行会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆