Skip to content

Commit 3396890

Browse files
committed
Propagate AccessDeniedException Only to ExceptionTranslationFilter
Closes gh-17761
1 parent c45bc38 commit 3396890

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/method/configuration/AuthorizationProxyWebConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp
102102
Throwable accessDeniedException = this.throwableAnalyzer
103103
.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
104104
if (accessDeniedException != null) {
105-
return new ModelAndView((model, req, res) -> {
106-
throw ex;
107-
});
105+
throw (AccessDeniedException) accessDeniedException;
108106
}
109107
return null;
110108
}

config/src/test/java/org/springframework/security/config/annotation/method/configuration/PrePostMethodSecurityConfigurationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.micrometer.observation.ObservationRegistry;
3434
import io.micrometer.observation.ObservationTextPublisher;
3535
import jakarta.annotation.security.DenyAll;
36+
import jakarta.servlet.RequestDispatcher;
3637
import org.aopalliance.aop.Advice;
3738
import org.aopalliance.intercept.MethodInterceptor;
3839
import org.aopalliance.intercept.MethodInvocation;
@@ -138,6 +139,7 @@
138139
import static org.assertj.core.api.Assertions.assertThat;
139140
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
140141
import static org.assertj.core.api.Assertions.assertThatNoException;
142+
import static org.hamcrest.Matchers.nullValue;
141143
import static org.mockito.ArgumentMatchers.any;
142144
import static org.mockito.Mockito.atLeastOnce;
143145
import static org.mockito.Mockito.clearInvocations;
@@ -149,6 +151,7 @@
149151
import static org.mockito.Mockito.verifyNoInteractions;
150152
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
151153
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
154+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
152155
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
153156

154157
/**
@@ -1279,6 +1282,19 @@ void getWhenPostAuthorizeAuthenticationNameNotMatchThenRespondsWithForbidden() t
12791282
this.mvc.perform(requestWithUser).andExpect(status().isForbidden());
12801283
}
12811284

1285+
// gh-17761
1286+
@Test
1287+
void getWhenPostAuthorizeAuthenticationNameNotMatchThenNoExceptionExposedInRequest() throws Exception {
1288+
this.spring.register(WebMvcMethodSecurityConfig.class, BasicController.class).autowire();
1289+
// @formatter:off
1290+
MockHttpServletRequestBuilder requestWithUser = get("/authorized-person")
1291+
.param("name", "john")
1292+
.with(user("rob"));
1293+
// @formatter:on
1294+
this.mvc.perform(requestWithUser)
1295+
.andExpect(request().attribute(RequestDispatcher.ERROR_EXCEPTION, nullValue()));
1296+
}
1297+
12821298
@Test
12831299
void getWhenPostAuthorizeWithinServiceAuthenticationNameMatchesThenRespondsWithOk() throws Exception {
12841300
this.spring.register(WebMvcMethodSecurityConfig.class, BasicController.class, BasicService.class).autowire();

0 commit comments

Comments
 (0)