1 | "hasAuthority('ROLE_ZZZZ')") ( |
当 hasAuthority('ROLE_ZZZZ')
认证失败时,会 throw AccessDeniedException 。
原本的期望是 该异常被自定义的 AccessDeniedHandler处理。但发现并非如此,他会直接抛出500异常。
在google 找到答案:
The access-denied-handler
is used by the ExceptionTranslationFilter
in case of an AccessDeniedException
. However, the org.springframework.web.servlet.DispatcherServlet
was first trying the handle the exception. Specifically, I had a org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
defined with a defaultErrorView
. Consequently, the SimpleMappingExceptionResolver
was consuming the exception by redirecting to an appropriate view, and consequently, there was no exception left to bubble up to the ExceptionTranslationFilter
.
The fix was rather simple. Configure the SimpleMappingExceptionResolver
to ignore all AccessDeniedException
.
1 | <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> |
Now, whenever an AccessDeniedException
is thrown, the resolver ignores it and allows it to bubble up the stack to the ExceptionTranslationFilter
which then calls upon the access-denied-handler
to handle the exception.