Spring Security如何使用exceptionMappings捕获PRE_AUTH_FILTER中的异常

gui*_*i42 7 spring exception spring-security

我正在使用Spring MVC(3.1.1.RELEASE)和Spring Security(3.1.0.RELEASE).

spring应用程序是一个REST API服务器,交给Json.

我的security-context.xml包含这个PRE_AUTH_FILTER过滤器:

<custom-filter position="PRE_AUTH_FILTER"  ref="siteminderFilter"  />
...
<beans:bean id="siteminderFilter" class=
"com.test.server.util.RequestHeaderAuthenticationFilter" >
    <beans:property name="principalRequestHeader" value="login"/>
    <beans:property name="authenticationManager" ref="authenticationManager" />    
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

我的自定义RequestHeaderAuthenticationFilter扩展了AbstractPreAuthenticatedProcessingFilter 并抛出了PreAuthenticatedCredentialsNotFoundException

因此用户收到500内部服务器错误.但我希望用户改为接收JSON字符串.可能吗 ?如果是的话,我该怎么做?

我试过这种方式使用exceptionMappings但它似乎没有用

<beans:bean id="siteminderFilter" class=
"com.pecunia.server.util.RequestHeaderAuthenticationFilter" >
    <beans:property name="principalRequestHeader" value="login"/>
    <beans:property name="authenticationManager" ref="authenticationManager" />    
   <beans:property name="exceptionMappings">
      <beans:props>
    <beans:prop key="org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException">/error.json</beans:prop>
    </beans:props>
   </beans:property>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

部署时给我这个错误:

Bean property 'exceptionMappings' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter
Run Code Online (Sandbox Code Playgroud)

我也试过了

<form-login authentication-failure-handler-ref="authenticationFailureHandler" />
...
<beans:bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
   <beans:property name="exceptionMappings">
      <beans:props>
    <beans:prop key="org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException">/error.json</beans:prop>
    </beans:props>
   </beans:property>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

但没有错误也没有结果(只是通常的500页)