|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.Map;
|
21 | 21 |
|
| 22 | +import javax.servlet.DispatcherType; |
22 | 23 | import javax.servlet.ServletRegistration;
|
23 | 24 |
|
24 | 25 | import org.junit.jupiter.api.Test;
|
|
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;
|
@@ -373,6 +376,29 @@ public void configureWhenUsingDefaultMatcherAndServletPathAndAuthorizationManage
|
373 | 376 | .configLocations(this.xml("DefaultMatcherServletPathAuthorizationManager")).autowire());
|
374 | 377 | }
|
375 | 378 |
|
| 379 | + @Test |
| 380 | + public void requestWhenUsingFilterAllDispatcherTypesAndAuthorizationManagerThenAuthorizesRequestsAccordingly() |
| 381 | + throws Exception { |
| 382 | + this.spring.configLocations(this.xml("AuthorizationManagerFilterAllDispatcherTypes")).autowire(); |
| 383 | + // @formatter:off |
| 384 | + this.mvc.perform(get("/path").with(userCredentials())) |
| 385 | + .andExpect(status().isOk()); |
| 386 | + this.mvc.perform(get("/path").with(adminCredentials())) |
| 387 | + .andExpect(status().isForbidden()); |
| 388 | + this.mvc.perform(get("/error").with((request) -> { |
| 389 | + request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 390 | + request.setDispatcherType(DispatcherType.ERROR); |
| 391 | + return request; |
| 392 | + })).andExpect(status().isOk()); |
| 393 | + this.mvc.perform(get("/path").with((request) -> { |
| 394 | + request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/path"); |
| 395 | + request.setDispatcherType(DispatcherType.ERROR); |
| 396 | + return request; |
| 397 | + })).andExpect(status().isUnauthorized()); |
| 398 | + // @formatter:on |
| 399 | + assertThat(this.spring.getContext().getBean(AuthorizationManager.class)).isNotNull(); |
| 400 | + } |
| 401 | + |
376 | 402 | private static RequestPostProcessor adminCredentials() {
|
377 | 403 | return httpBasic("admin", "password");
|
378 | 404 | }
|
@@ -410,6 +436,16 @@ String path(@PathVariable("un") String name) {
|
410 | 436 |
|
411 | 437 | }
|
412 | 438 |
|
| 439 | + @RestController |
| 440 | + static class ErrorController { |
| 441 | + |
| 442 | + @GetMapping("/error") |
| 443 | + String error() { |
| 444 | + return "error"; |
| 445 | + } |
| 446 | + |
| 447 | + } |
| 448 | + |
413 | 449 | public static class Id {
|
414 | 450 |
|
415 | 451 | public boolean isOne(int i) {
|
|
0 commit comments