|
19 | 19 | import java.io.Serializable;
|
20 | 20 | import java.lang.reflect.Method;
|
21 | 21 | import java.lang.reflect.Modifier;
|
| 22 | +import java.util.ArrayList; |
22 | 23 | import java.util.List;
|
23 | 24 |
|
| 25 | +import javax.servlet.Filter; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | + |
24 | 28 | import org.junit.Rule;
|
25 | 29 | import org.junit.Test;
|
26 | 30 |
|
@@ -131,6 +135,19 @@ public void loadConfigWhenSecurityFilterChainsHaveOrderThenFilterChainsOrdered()
|
131 | 135 | assertThat(filterChains.get(3).matches(request)).isTrue();
|
132 | 136 | }
|
133 | 137 |
|
| 138 | + @Test |
| 139 | + public void loadConfigWhenSecurityFilterChainsHaveOrderOnBeanDefinitionsThenFilterChainsOrdered() { |
| 140 | + this.spring.register(OrderOnBeanDefinitionsSecurityFilterChainConfig.class).autowire(); |
| 141 | + FilterChainProxy filterChainProxy = this.spring.getContext().getBean(FilterChainProxy.class); |
| 142 | + List<SecurityFilterChain> filterChains = filterChainProxy.getFilterChains(); |
| 143 | + assertThat(filterChains).hasSize(2); |
| 144 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); |
| 145 | + request.setServletPath("/role1/**"); |
| 146 | + assertThat(filterChains.get(0).matches(request)).isTrue(); |
| 147 | + request.setServletPath("/role2/**"); |
| 148 | + assertThat(filterChains.get(1).matches(request)).isTrue(); |
| 149 | + } |
| 150 | + |
134 | 151 | @Test
|
135 | 152 | public void loadConfigWhenWebSecurityConfigurersHaveSameOrderThenThrowBeanCreationException() {
|
136 | 153 | assertThatExceptionOfType(BeanCreationException.class)
|
@@ -472,6 +489,45 @@ SecurityFilterChain filterChain4(HttpSecurity http) throws Exception {
|
472 | 489 |
|
473 | 490 | }
|
474 | 491 |
|
| 492 | + @EnableWebSecurity |
| 493 | + @Import(AuthenticationTestConfiguration.class) |
| 494 | + static class OrderOnBeanDefinitionsSecurityFilterChainConfig { |
| 495 | + |
| 496 | + @Bean |
| 497 | + @Order(1) |
| 498 | + SecurityFilterChain securityFilterChain1(HttpSecurity http) throws Exception { |
| 499 | + // @formatter:off |
| 500 | + return http |
| 501 | + .antMatcher("/role1/**") |
| 502 | + .authorizeRequests((authorize) -> authorize |
| 503 | + .anyRequest().hasRole("1") |
| 504 | + ) |
| 505 | + .build(); |
| 506 | + // @formatter:on |
| 507 | + } |
| 508 | + |
| 509 | + @Bean |
| 510 | + TestSecurityFilterChain securityFilterChain2(HttpSecurity http) throws Exception { |
| 511 | + return new TestSecurityFilterChain(); |
| 512 | + } |
| 513 | + |
| 514 | + @Order(2) |
| 515 | + static class TestSecurityFilterChain implements SecurityFilterChain { |
| 516 | + |
| 517 | + @Override |
| 518 | + public boolean matches(HttpServletRequest request) { |
| 519 | + return true; |
| 520 | + } |
| 521 | + |
| 522 | + @Override |
| 523 | + public List<Filter> getFilters() { |
| 524 | + return new ArrayList<>(); |
| 525 | + } |
| 526 | + |
| 527 | + } |
| 528 | + |
| 529 | + } |
| 530 | + |
475 | 531 | @EnableWebSecurity
|
476 | 532 | @Import(AuthenticationTestConfiguration.class)
|
477 | 533 | static class DuplicateOrderConfig {
|
|
0 commit comments