Skip to content

Commit 27574ee

Browse files
committed
Polish UserDetailsService Selection Logic
This commit rearranges the branches to reduce nesting Signed-off-by: Josh Cummings <3627351+jzheaux@users.noreply.github.com>
1 parent 57588c4 commit 27574ee

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

config/src/main/java/org/springframework/security/config/annotation/authentication/configuration/InitializeUserDetailsBeanManagerConfigurer.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,30 @@ public void configure(AuthenticationManagerBuilder auth) {
7070
String[] beanNames = InitializeUserDetailsBeanManagerConfigurer.this.context
7171
.getBeanNamesForType(UserDetailsService.class);
7272

73-
if (auth.isConfigured()) {
74-
if (beanNames.length > 0) {
75-
this.logger.warn("Global AuthenticationManager configured with an AuthenticationProvider bean. "
76-
+ "UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. "
77-
+ "Consider removing the AuthenticationProvider bean. "
78-
+ "Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. "
79-
+ "If the current configuration is intentional, to turn off this warning, "
80-
+ "increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR");
81-
}
73+
if (beanNames.length == 0) {
8274
return;
8375
}
8476

85-
if (beanNames.length == 0) {
77+
if (auth.isConfigured()) {
78+
this.logger.warn("Global AuthenticationManager configured with an AuthenticationProvider bean. "
79+
+ "UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. "
80+
+ "Consider removing the AuthenticationProvider bean. "
81+
+ "Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. "
82+
+ "If the current configuration is intentional, to turn off this warning, "
83+
+ "increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR");
8684
return;
8785
}
8886

8987
// Try to resolve a single candidate using the container's rules (@Primary,
9088
// etc.)
91-
UserDetailsService userDetailsService = getAutowireCandidateOrNull(UserDetailsService.class);
89+
UserDetailsService userDetailsService = getBeanIfUnique(UserDetailsService.class);
9290

9391
// If ambiguous or otherwise not resolvable, keep the warn-and-skip behavior
9492
if (userDetailsService == null) {
95-
if (beanNames.length > 1) {
96-
this.logger.warn(LogMessage.format("Found %s UserDetailsService beans, with names %s. "
97-
+ "Global Authentication Manager will not use a UserDetailsService for username/password login. "
98-
+ "Consider publishing a single (or primary) UserDetailsService bean.", beanNames.length,
99-
Arrays.toString(beanNames)));
100-
}
93+
this.logger.warn(LogMessage.format("Found %s UserDetailsService beans, with names %s. "
94+
+ "Global Authentication Manager will not use a UserDetailsService for username/password login. "
95+
+ "Consider publishing a single (or primary) UserDetailsService bean.", beanNames.length,
96+
Arrays.toString(beanNames)));
10197
return;
10298
}
10399

config/src/test/java/org/springframework/security/config/annotation/authentication/configuration/InitializeUserDetailsBeanManagerConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ void whenMultipleUdsAndOneResolvableCandidate_thenPrimaryIsAutoWired() throws Ex
6565
ObjectProvider<UserDetailsService> udsProvider = (ObjectProvider<UserDetailsService>) mock(
6666
ObjectProvider.class);
6767
given(ctx.getBeanProvider(UserDetailsService.class)).willReturn(udsProvider);
68-
given(udsProvider.getIfAvailable()).willReturn(primary); // container picks single
69-
// candidate
68+
given(udsProvider.getIfUnique()).willReturn(primary); // container picks single
69+
// candidate
7070

7171
// resolveBeanName(..) path
7272
given(ctx.getBean("udsA")).willReturn(secondary);

0 commit comments

Comments
 (0)