java – 在Spring webflow中捕获“死”会话
作者:互联网
我想抓住当我松开会话时抛出的异常,不仅仅是因为会话超时(例如报告).
此外,我希望此处理程序将只处理该特定异常,并且不会是全局异常处理程序或任何类似的事情.
基本上,我想捕获异常org.springframework.web.HttpSessionRequiredException.
解决方法:
使用下面提出的任何解决方案,您应该能够处理异常并对它们执行逻辑.
可能的解决方案:
1.您可以添加一个FlowExecutionListenerAdapter,它将监听抛出的所有异常,然后您可以按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 executor配置中添加它:
<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.虽然它将是一个全局异常处理程序的方法
public void handle(FlowExecutionException exception,
RequestControlContext context) { }
将允许您访问允许您过滤许多不同变量的上下文.另见,How implement the interface FlowExecutionExceptionHandler
3.如果您正在使用Spring Security项目,另一种可能的解决方案我相信如果您有以下配置,Spring安全性将自动为您处理异常:
<security:http auto-config="true" use-expressions="true">
<!-- rest of config omitted -->
<security:session-management invalid-session-url="/login"/>
</security:http>
如果您想捕获异常并对其执行逻辑,请参阅以下答案:Logout/Session timeout catching with spring security
我会推荐第三种解决方案,因为它是最不具侵入性的.在第3个解决方案的链接中,有几个非常优雅的解决方案来处理会话.
标签:java,spring,exception-handling,session,spring-webflow 来源: https://codeday.me/bug/20191003/1846543.html