Skip to content

Commit 8cefc8a

Browse files
committed
Fix bug with multiple AuthenticationManager beans
Closes gh-9256
1 parent 7dde7cf commit 8cefc8a

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.java

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcessor) {
6060
this.objectPostProcessor = objectPostProcessor;
6161
}
6262

63-
@Autowired(required = false)
6463
void setAuthenticationManager(AuthenticationManager authenticationManager) {
6564
this.authenticationManager = authenticationManager;
6665
}

config/src/test/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurationTests.java

+86-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@
4141
import org.springframework.security.access.expression.SecurityExpressionHandler;
4242
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
4343
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;
4447
import org.springframework.security.authentication.TestingAuthenticationToken;
48+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
4549
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
4650
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
4751
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4852
import org.springframework.security.config.annotation.web.builders.WebSecurity;
4953
import org.springframework.security.config.test.SpringTestRule;
5054
import org.springframework.security.config.users.AuthenticationTestConfiguration;
5155
import org.springframework.security.core.Authentication;
56+
import org.springframework.security.core.AuthenticationException;
5257
import org.springframework.security.web.FilterChainProxy;
5358
import org.springframework.security.web.FilterInvocation;
5459
import org.springframework.security.web.SecurityFilterChain;
@@ -253,7 +258,6 @@ public void loadConfigWhenBothAdapterAndFilterChainConfiguredThenException() {
253258
.isThrownBy(() -> this.spring.register(AdapterAndFilterChainConfig.class).autowire())
254259
.withRootCauseExactlyInstanceOf(IllegalStateException.class)
255260
.withMessageContaining("Found WebSecurityConfigurerAdapter as well as SecurityFilterChain.");
256-
257261
}
258262

259263
@Test
@@ -341,6 +345,19 @@ public void loadConfigWhenCustomizersHaveOrderThenCustomizersOrdered() {
341345
assertThat(filterChains.get(1).getFilters()).isEmpty();
342346
}
343347

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+
344361
@EnableWebSecurity
345362
@Import(AuthenticationTestConfiguration.class)
346363
static class SortedWebSecurityConfigurerAdaptersConfig {
@@ -867,4 +884,72 @@ public WebSecurityCustomizer webSecurityCustomizer2() {
867884

868885
}
869886

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+
870955
}

0 commit comments

Comments
 (0)