开发者

Access Spring Web MVC Exception Resolver from Spring Security Context

开发者 https://www.devze.com 2023-03-12 22:38 出处:网络
I have a Spring Web MVC configuration with a SimpleMappingExceptionResolver in it to handle some access exceptions:

I have a Spring Web MVC configuration with a SimpleMappingExceptionResolver in it to handle some access exceptions:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException">
    <property name="exceptionMappings">
        <props>
            <prop key=".DataAccessException">dataAccessFailure</prop>
            <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
            <prop key=".TypeMismatchException">resourceNotFound</prop>
            <prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
        </props>
    </property>
</bean>

I also have a Spring Security context configuration where I would like to handle some authentication related exceptions. Currently, I have an ExceptionMappingAuthenticationFailureHandler set up as follows:

<form-login authentication-failure-handler-ref="exceptionMapper" ... />

...

<bean id="exceptionMapper" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler" >
    <property name="exceptionMappings">
        <props>
            <prop key=".CredentialsExpiredException">/resetPassword</prop>
            <prop key=".BadCredentialsException">/login?failure=true</prop>
        </props>
    </property>
</bean>

I was thinking that it would be nice to consolidate these into a single exception handling configuration开发者_开发问答 by moving the security mappings to the MVC configuration. My problem is that I don't know how to tell Spring Security that I want form-login's authentication-failure-handler to use the resolver.

I can't just add an id to SimpleMappingExceptionResolver because 1) authentication-failure-handler-ref expects a Handler, not a Resolver, and 2) any beans that are defined in the MVC configuration don't seem to be visible from the security context...

Thanks for any help!


To answer part 2) of your question, you can share bean definitions between configurations via the use of the <import resource="..."/> tag.

I'm not sure how successful you will be for part 1) (single exception resolver), because having looked at the API both classes are completely different - no shared interface. You may have to roll your own, using one of the classes as the platform and building the functionality of the other class into it.

0

精彩评论

暂无评论...
验证码 换一张
取 消