Skip to content

Commit 61e9b00

Browse files
committed
Handle AuthorizationDeniedException wrapped in ServletException
Closes: gh-17761 Signed-off-by: Andrey Litvitski <[email protected]>
1 parent aeb2dbc commit 61e9b00

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

web/src/test/java/org/springframework/security/web/access/ExceptionTranslationFilterTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
4040
import org.springframework.security.authentication.BadCredentialsException;
4141
import org.springframework.security.authentication.RememberMeAuthenticationToken;
42+
import org.springframework.security.authorization.AuthorizationDeniedException;
4243
import org.springframework.security.core.AuthenticationException;
4344
import org.springframework.security.core.authority.AuthorityUtils;
4445
import org.springframework.security.core.context.SecurityContext;
@@ -288,6 +289,20 @@ public void setMessageSourceWhenNotNullThenCanGet() {
288289
verify(source).getMessage(eq(code), any(), any());
289290
}
290291

292+
@Test
293+
public void servletExceptionWrappingAuthorizationDeniedExceptionIsRethrown() throws Exception {
294+
MockHttpServletRequest request = get("/secure/page.html").build();
295+
MockHttpServletResponse response = new MockHttpServletResponse();
296+
FilterChain fc = mockFilterChainWithException(new ServletException(new AuthorizationDeniedException("Denied")));
297+
SecurityContextHolder.getContext()
298+
.setAuthentication(new AnonymousAuthenticationToken("ignored", "ignored",
299+
AuthorityUtils.createAuthorityList("IGNORED")));
300+
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint);
301+
assertThatExceptionOfType(ServletException.class)
302+
.isThrownBy(() -> filter.doFilter(request, response, fc))
303+
.withCauseInstanceOf(AuthorizationDeniedException.class);
304+
}
305+
291306
private FilterChain mockFilterChainWithException(Exception exception) throws ServletException, IOException {
292307
FilterChain fc = mock(FilterChain.class);
293308
willThrow(exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));

0 commit comments

Comments
 (0)