Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContext;
Expand Down Expand Up @@ -288,6 +289,20 @@ public void setMessageSourceWhenNotNullThenCanGet() {
verify(source).getMessage(eq(code), any(), any());
}

@Test
public void servletExceptionWrappingAuthorizationDeniedExceptionIsRethrown() throws Exception {
MockHttpServletRequest request = get("/secure/page.html").build();
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain fc = mockFilterChainWithException(new ServletException(new AuthorizationDeniedException("Denied")));
SecurityContextHolder.getContext()
.setAuthentication(new AnonymousAuthenticationToken("ignored", "ignored",
AuthorityUtils.createAuthorityList("IGNORED")));
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint);
assertThatExceptionOfType(ServletException.class)
.isThrownBy(() -> filter.doFilter(request, response, fc))
.withCauseInstanceOf(AuthorizationDeniedException.class);
}

private FilterChain mockFilterChainWithException(Exception exception) throws ServletException, IOException {
FilterChain fc = mock(FilterChain.class);
willThrow(exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
Expand Down
Loading