首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Spring web flow、Spring Security和像SimpleMappingExceptionResolver这样的HandlerExceptionResolver结合起来?

如何将Spring web flow、Spring Security和像SimpleMappingExceptionResolver这样的HandlerExceptionResolver结合起来?
EN

Stack Overflow用户
提问于 2013-01-30 05:48:44
回答 1查看 1.6K关注 0票数 3

我有一个SimpleMappingExceptionResolver来重定向每个未处理的异常。

代码语言:javascript
复制
 @Bean public SimpleMappingExceptionResolver exceptionResolver() {
           SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
           resolver.setDefaultErrorView("general-error");
           resolver.setWarnLogCategory(TamponException.class.getName());
           return resolver;
}

一旦我实现了Spring安全,我就意识到我需要排除AccessDeniedException

代码语言:javascript
复制
resolver.setExcludedExceptions(AccessDeniedException.class);

现在我正在实现Spring Web Flow。SWF在FlowExecutionException中包装了这些基本的AccessDeniedException。这种组合破坏了Spring Security,因为那些被包装的异常现在被SimpleMappingExceptionResolver捕获。我也可以排除FlowExecutionException,但这不是我想要的。

我如何正确地解决这个问题?

我的下一个想法是实现一个HandlerExceptionResolver,它仅在未包装的异常不是AccessDeniedException的情况下才委托resolveException()。但是我想知道是不是没有什么东西可以用来组合SWF,Security和HandlerExceptionResolver。

EN

回答 1

Stack Overflow用户

发布于 2013-04-08 23:21:23

我使用的配置与您的类似,具有Spring webflow和Spring安全性。为了处理异常,我使用webflow处理而不是SimpleMappingExceptionResolver,这对我来说工作得很好。

首先,您需要一个处理异常的全局xml流,此流将用作所有其他流的“父”。或者,您也可以在流程中直接包含全局转换和视图状态:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
      http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
      abstract="true">

    <persistence-context/>

    <view-state id="generalException" view="../views/exception/generalException.xhtml">
        <on-entry>
            <evaluate expression="exceptionManager.extractMessages(flowExecutionException, rootCauseException)" result="viewScope.exc"/>
        </on-entry>
    </view-state>

    <global-transitions>
        <transition on-exception="java.lang.Exception" to="generalException"/>
    </global-transitions>

</flow>

ExceptionManager类仅用于以可读的方式格式化异常,特别是在我的示例BatchUpdateException中,它需要调用next()方法才能知道异常的来源:

代码语言:javascript
复制
@Service("exceptionManager")
public class ExceptionManagerImpl {

    public Map<String, String> extractMessages(Exception e, Exception root)
    {
        Map<String, String> out = new HashMap<String, String>();

        out.put("exc_message", e.getClass().toString() + ": " + e.getMessage());
        out.put("exc_details", formatStackTrace(e.getStackTrace()));
        out.put("root_message", root.getClass().toString() + ": " + root.getMessage());
        out.put("root_details", formatStackTrace(root.getStackTrace()));
        if (root instanceof BatchUpdateException)
        {
            out.put("batch_message", ((BatchUpdateException)root).getNextException().getClass().toString() + ": " + ((BatchUpdateException)root).getNextException().getMessage());
        }

        return out;
    }

    public String formatStackTrace(StackTraceElement[] elements)
    {
        String out = "";
        for (StackTraceElement ste: elements)
            out += ste.toString() + "<br/>";
        return out;
    }
}

通过这种方式,所有未处理的异常都将显示在JSF页面中,或者用于视图的任何内容中。通过这个实现,AccessDeniedException在我的系统中正常地通过了Spring安全性。您还可以为不同的异常指定不同的行为。

希望对你有帮助,祝你愉快,

马蒂亚

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14592933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档