gpt4 book ai didi

java - 在 Spring webflow 中捕获 "dead" session

转载 作者:行者123 更新时间:2023-11-30 08:14:06 30 4
gpt4 key购买 nike

我想捕获当我失去 session 时抛出的异常,而不仅仅是因为 session 超时(例如对于报告)。另外,我希望该处理程序仅处理该特定异常,而不是全局异常处理程序或类似的东西。基本上,我想捕获异常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 执行器配置中:

<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 会自动为您处理异常:

<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

我会推荐第三种解决方案,因为它是干扰最小的。在第三个解决方案的链接中,有几个非常优雅的解决方案来处理 session 。

关于java - 在 Spring webflow 中捕获 "dead" session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901635/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com