|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.Map;
|
21 | 21 |
|
| 22 | +import jakarta.servlet.DispatcherType; |
22 | 23 | import jakarta.servlet.ServletRegistration;
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 | import org.junit.jupiter.api.extension.ExtendWith;
|
|
33 | 34 | import org.springframework.security.config.test.SpringTestContextExtension;
|
34 | 35 | import org.springframework.test.web.servlet.MockMvc;
|
35 | 36 | import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
| 37 | +import org.springframework.web.bind.annotation.GetMapping; |
36 | 38 | import org.springframework.web.bind.annotation.PathVariable;
|
37 | 39 | import org.springframework.web.bind.annotation.RequestMapping;
|
38 | 40 | import org.springframework.web.bind.annotation.RestController;
|
39 | 41 | import org.springframework.web.context.ConfigurableWebApplicationContext;
|
| 42 | +import org.springframework.web.util.WebUtils; |
40 | 43 |
|
41 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 45 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -380,6 +383,29 @@ public void configureWhenUsingDefaultMatcherAndServletPathAndAuthorizationManage
|
380 | 383 | .configLocations(this.xml("DefaultMatcherServletPathAuthorizationManager")).autowire());
|
381 | 384 | }
|
382 | 385 |
|
| 386 | + @Test |
| 387 | + public void requestWhenUsingFilterAllDispatcherTypesAndAuthorizationManagerThenAuthorizesRequestsAccordingly() |
| 388 | + throws Exception { |
| 389 | + this.spring.configLocations(this.xml("AuthorizationManagerFilterAllDispatcherTypes")).autowire(); |
| 390 | + // @formatter:off |
| 391 | + this.mvc.perform(get("/path").with(userCredentials())) |
| 392 | + .andExpect(status().isOk()); |
| 393 | + this.mvc.perform(get("/path").with(adminCredentials())) |
| 394 | + .andExpect(status().isForbidden()); |
| 395 | + this.mvc.perform(get("/error").with((request) -> { |
| 396 | + request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 397 | + request.setDispatcherType(DispatcherType.ERROR); |
| 398 | + return request; |
| 399 | + })).andExpect(status().isOk()); |
| 400 | + this.mvc.perform(get("/path").with((request) -> { |
| 401 | + request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/path"); |
| 402 | + request.setDispatcherType(DispatcherType.ERROR); |
| 403 | + return request; |
| 404 | + })).andExpect(status().isUnauthorized()); |
| 405 | + // @formatter:on |
| 406 | + assertThat(this.spring.getContext().getBean(AuthorizationManager.class)).isNotNull(); |
| 407 | + } |
| 408 | + |
383 | 409 | private static RequestPostProcessor adminCredentials() {
|
384 | 410 | return httpBasic("admin", "password");
|
385 | 411 | }
|
@@ -417,6 +443,16 @@ String path(@PathVariable("un") String name) {
|
417 | 443 |
|
418 | 444 | }
|
419 | 445 |
|
| 446 | + @RestController |
| 447 | + static class ErrorController { |
| 448 | + |
| 449 | + @GetMapping("/error") |
| 450 | + String error() { |
| 451 | + return "error"; |
| 452 | + } |
| 453 | + |
| 454 | + } |
| 455 | + |
420 | 456 | public static class Id {
|
421 | 457 |
|
422 | 458 | public boolean isOne(int i) {
|
|
0 commit comments