|
41 | 41 | import org.springframework.security.access.expression.SecurityExpressionHandler;
|
42 | 42 | import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
43 | 43 | import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
|
| 44 | +import org.springframework.security.authentication.AuthenticationManager; |
| 45 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 46 | +import org.springframework.security.authentication.ProviderManager; |
44 | 47 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 48 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
45 | 49 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
46 | 50 | import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
|
47 | 51 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
48 | 52 | import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
49 | 53 | import org.springframework.security.config.test.SpringTestRule;
|
50 | 54 | import org.springframework.security.config.users.AuthenticationTestConfiguration;
|
51 | 55 | import org.springframework.security.core.Authentication;
|
| 56 | +import org.springframework.security.core.AuthenticationException; |
52 | 57 | import org.springframework.security.web.FilterChainProxy;
|
53 | 58 | import org.springframework.security.web.FilterInvocation;
|
54 | 59 | import org.springframework.security.web.SecurityFilterChain;
|
@@ -253,7 +258,6 @@ public void loadConfigWhenBothAdapterAndFilterChainConfiguredThenException() {
|
253 | 258 | .isThrownBy(() -> this.spring.register(AdapterAndFilterChainConfig.class).autowire())
|
254 | 259 | .withRootCauseExactlyInstanceOf(IllegalStateException.class)
|
255 | 260 | .withMessageContaining("Found WebSecurityConfigurerAdapter as well as SecurityFilterChain.");
|
256 |
| - |
257 | 261 | }
|
258 | 262 |
|
259 | 263 | @Test
|
@@ -341,6 +345,19 @@ public void loadConfigWhenCustomizersHaveOrderThenCustomizersOrdered() {
|
341 | 345 | assertThat(filterChains.get(1).getFilters()).isEmpty();
|
342 | 346 | }
|
343 | 347 |
|
| 348 | + @Test |
| 349 | + public void loadConfigWhenMultipleAuthenticationManagersAndWebSecurityConfigurerAdapterThenConfigurationApplied() { |
| 350 | + this.spring.register(MultipleAuthenticationManagersConfig.class).autowire(); |
| 351 | + FilterChainProxy filterChainProxy = this.spring.getContext().getBean(FilterChainProxy.class); |
| 352 | + List<SecurityFilterChain> filterChains = filterChainProxy.getFilterChains(); |
| 353 | + assertThat(filterChains).hasSize(2); |
| 354 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); |
| 355 | + request.setServletPath("/role1"); |
| 356 | + assertThat(filterChains.get(0).matches(request)).isTrue(); |
| 357 | + request.setServletPath("/role2"); |
| 358 | + assertThat(filterChains.get(1).matches(request)).isTrue(); |
| 359 | + } |
| 360 | + |
344 | 361 | @EnableWebSecurity
|
345 | 362 | @Import(AuthenticationTestConfiguration.class)
|
346 | 363 | static class SortedWebSecurityConfigurerAdaptersConfig {
|
@@ -867,4 +884,72 @@ public WebSecurityCustomizer webSecurityCustomizer2() {
|
867 | 884 |
|
868 | 885 | }
|
869 | 886 |
|
| 887 | + @EnableWebSecurity |
| 888 | + static class MultipleAuthenticationManagersConfig { |
| 889 | + |
| 890 | + @Bean("authManager1") |
| 891 | + static AuthenticationManager authenticationManager1() { |
| 892 | + return new ProviderManager(new AuthenticationProvider() { |
| 893 | + @Override |
| 894 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 895 | + return new UsernamePasswordAuthenticationToken("user", "credentials"); |
| 896 | + } |
| 897 | + |
| 898 | + @Override |
| 899 | + public boolean supports(Class<?> authentication) { |
| 900 | + return false; |
| 901 | + } |
| 902 | + }); |
| 903 | + } |
| 904 | + |
| 905 | + @Bean("authManager2") |
| 906 | + static AuthenticationManager authenticationManager2() { |
| 907 | + return new ProviderManager(new AuthenticationProvider() { |
| 908 | + @Override |
| 909 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 910 | + return new UsernamePasswordAuthenticationToken("subuser", "credentials"); |
| 911 | + } |
| 912 | + |
| 913 | + @Override |
| 914 | + public boolean supports(Class<?> authentication) { |
| 915 | + return false; |
| 916 | + } |
| 917 | + }); |
| 918 | + } |
| 919 | + |
| 920 | + @Configuration |
| 921 | + @Order(1) |
| 922 | + public static class SecurityConfig1 extends WebSecurityConfigurerAdapter { |
| 923 | + |
| 924 | + @Override |
| 925 | + protected AuthenticationManager authenticationManager() { |
| 926 | + return authenticationManager1(); |
| 927 | + } |
| 928 | + |
| 929 | + @Override |
| 930 | + protected void configure(HttpSecurity http) throws Exception { |
| 931 | + // @formatter:off |
| 932 | + http |
| 933 | + .antMatcher("/role1/**") |
| 934 | + .authorizeRequests((authorize) -> authorize |
| 935 | + .anyRequest().hasRole("1") |
| 936 | + ); |
| 937 | + // @formatter:on |
| 938 | + } |
| 939 | + |
| 940 | + } |
| 941 | + |
| 942 | + @Configuration |
| 943 | + @Order(2) |
| 944 | + public static class SecurityConfig2 extends WebSecurityConfigurerAdapter { |
| 945 | + |
| 946 | + @Override |
| 947 | + protected AuthenticationManager authenticationManager() { |
| 948 | + return authenticationManager2(); |
| 949 | + } |
| 950 | + |
| 951 | + } |
| 952 | + |
| 953 | + } |
| 954 | + |
870 | 955 | }
|
0 commit comments