2828import org .springframework .context .annotation .Role ;
2929import org .springframework .http .HttpEntity ;
3030import org .springframework .http .ResponseEntity ;
31- import org .springframework .http .converter .HttpMessageNotWritableException ;
3231import org .springframework .security .access .AccessDeniedException ;
3332import org .springframework .security .authorization .method .AuthorizationAdvisorProxyFactory ;
3433import org .springframework .security .web .util .ThrowableAnalyzer ;
@@ -52,11 +51,11 @@ public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resol
5251 for (int i = 0 ; i < resolvers .size (); i ++) {
5352 HandlerExceptionResolver resolver = resolvers .get (i );
5453 if (resolver instanceof DefaultHandlerExceptionResolver ) {
55- resolvers .add (i , new HttpMessageNotWritableAccessDeniedExceptionResolver ());
54+ resolvers .add (i , new AccessDeniedExceptionResolver ());
5655 return ;
5756 }
5857 }
59- resolvers .add (new HttpMessageNotWritableAccessDeniedExceptionResolver ());
58+ resolvers .add (new AccessDeniedExceptionResolver ());
6059 }
6160
6261 static class WebTargetVisitor implements AuthorizationAdvisorProxyFactory .TargetVisitor {
@@ -84,24 +83,20 @@ public Object visit(AuthorizationAdvisorProxyFactory proxyFactory, Object target
8483
8584 }
8685
87- static class HttpMessageNotWritableAccessDeniedExceptionResolver implements HandlerExceptionResolver {
86+ static class AccessDeniedExceptionResolver implements HandlerExceptionResolver {
8887
8988 final ThrowableAnalyzer throwableAnalyzer = new ThrowableAnalyzer ();
9089
9190 @ Override
9291 public ModelAndView resolveException (HttpServletRequest request , HttpServletResponse response , Object handler ,
9392 Exception ex ) {
94- // Only resolves AccessDeniedException if it occurred during serialization,
95- // otherwise lets the user-defined handler deal with it.
96- if (ex instanceof HttpMessageNotWritableException ) {
97- Throwable [] causeChain = this .throwableAnalyzer .determineCauseChain (ex );
98- Throwable accessDeniedException = this .throwableAnalyzer
99- .getFirstThrowableOfType (AccessDeniedException .class , causeChain );
100- if (accessDeniedException != null ) {
101- return new ModelAndView ((model , req , res ) -> {
102- throw ex ;
103- });
104- }
93+ Throwable [] causeChain = this .throwableAnalyzer .determineCauseChain (ex );
94+ Throwable accessDeniedException = this .throwableAnalyzer
95+ .getFirstThrowableOfType (AccessDeniedException .class , causeChain );
96+ if (accessDeniedException != null ) {
97+ return new ModelAndView ((model , req , res ) -> {
98+ throw ex ;
99+ });
105100 }
106101 return null ;
107102 }
0 commit comments