spring异常处理程序不处理某些类型的异常 [英] spring exception handler to not handle certain types of exception

查看:177
本文介绍了spring异常处理程序不处理某些类型的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的spring 2.5应用程序中设置一个简单的异常处理程序。目前,它捕获所有异常 s并显示一个堆栈跟踪页面。



这是很好,但现在春天的安全没有正确地踢未登录的用户到登录页面,而是我的例外页面显示与春天的安全例外:

  org.springframework.security.AccessDeniedException 

问题是该应用程序没有自己的Exception子类,它使用所有的异常,所以我必须映射异常,但取消映射 AccessDeniedException



是否可能在春季2.5?



编辑:使用spring security 2.0.1



我的bean看起来像这样

 < bean class =org.springframework.web.servlet .handler.SimpleMappingExceptionResolver> 
< property name =exceptionMappings>
<道具>
< prop key =java.lang.RuntimeException> common / error< / prop>
< / props>
< / property>
< / bean> **


解决方案

我们处理这个的方法是有一个自定义异常解析器类来处理任何不被其他处理程序捕获的异常 - 它实现了HandlerExceptionResolver,Ordered。



我们声明一个单独的SimpleMappingExceptionResolver bean捕获特定的异常。



排序是这样的,我们的自定义解析器在SimpleMappingExceptionResolver之后运行。



效果是指定的异常(例如AccessDeniedException)由SimpleMappingExceptionResolver处理并定向到相应的页面。



任何其他运行时异常都由自定义解析器处理,到一个通用错误页面。

 < bean class =org.springframework.web.servlet.handler.SimpleMappingExceptionResolver> 
< property name =exceptionMappings>
<道具>
< prop key =org.springframework.security.AccessDeniedException> accessDenied< / prop>
< prop key =org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException> accessDenied< / prop>
< / props>
< / property>
< property name =ordervalue =0/>
< / bean>

< bean class =package.to.your.handler.DefaultExceptionResolver>
< property name =ordervalue =1/>
< / bean>

这个安排允许你捕获尽可能多的异常(我在这里抓住2)AccessDenied和HibernateOptimisticLockingFailureException )使用Spring解析器,其他一切都被定制解析器捕获。在上面接受的解决方案中,您必须编写更多的Java代码才能捕获AccessDenied以外的异常。


ive set up a simple exception handler in my spring 2.5 app. Currently it catches all Exceptions and shows a stacktrace page.

this is well and good, but now spring security does not properly kick the non-logged in user to the login page, instead my exception page is shown with the spring security exception:

org.springframework.security.AccessDeniedException

The problem is that this application doesnt have its own Exception subclass that it uses for all its Exceptions, so i must map Exception but unmap AccessDeniedException

is this possible in spring 2.5?

edit: with spring security 2.0.1

my bean looks like this

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.RuntimeException">common/error</prop>
        </props>
    </property>
</bean>**

解决方案

The way we handle this is to have a custom exception resolver class that handles any exceptions that are not caught by other handlers - it implements HandlerExceptionResolver, Ordered.

We declare a separate SimpleMappingExceptionResolver bean that catches specific exceptions.

The ordering is such that our custom resolver runs after the SimpleMappingExceptionResolver.

The effect is that specified exceptions (e.g. AccessDeniedException) are handled by SimpleMappingExceptionResolver and directed to the appropriate pages.

Any other run time exceptions are handled by the custom resolver, which forwards to a generic error page.

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="org.springframework.security.AccessDeniedException">accessDenied</prop>
            <prop key="org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException">accessDenied</prop>
        </props>
    </property>
    <property name="order" value="0"/>
</bean>

<bean class="package.to.your.handler.DefaultExceptionResolver">
    <property name="order" value="1"/>
</bean>

This arrangement allows you to catch as many exceptions as you like (I catch 2 here, AccessDenied and HibernateOptimisticLockingFailureException) using the Spring resolver and everything else is caught by the custom resolver. In the accepted solution above, you would have to write more Java code to catch exceptions other than AccessDenied.

这篇关于spring异常处理程序不处理某些类型的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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