1616
1717package org .springframework .security .config .annotation .authentication .configuration ;
1818
19- import java .util .ArrayList ;
20- import java .util .List ;
19+ import java .util .Arrays ;
2120
2221import org .apache .commons .logging .Log ;
2322import org .apache .commons .logging .LogFactory ;
@@ -66,9 +65,10 @@ class InitializeUserDetailsManagerConfigurer extends GlobalAuthenticationConfigu
6665
6766 @ Override
6867 public void configure (AuthenticationManagerBuilder auth ) throws Exception {
69- List <BeanWithName <UserDetailsService >> userDetailsServices = getBeansWithName (UserDetailsService .class );
68+ String [] beanNames = InitializeUserDetailsBeanManagerConfigurer .this .context
69+ .getBeanNamesForType (UserDetailsService .class );
7070 if (auth .isConfigured ()) {
71- if (! userDetailsServices . isEmpty () ) {
71+ if (beanNames . length > 0 ) {
7272 this .logger .warn ("Global AuthenticationManager configured with an AuthenticationProvider bean. "
7373 + "UserDetailsService beans will not be used for username/password login. "
7474 + "Consider removing the AuthenticationProvider bean. "
@@ -78,19 +78,18 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
7878 return ;
7979 }
8080
81- if (userDetailsServices . isEmpty () ) {
81+ if (beanNames . length == 0 ) {
8282 return ;
8383 }
84- else if (userDetailsServices .size () > 1 ) {
85- List <String > beanNames = userDetailsServices .stream ().map (BeanWithName ::getName ).toList ();
84+ else if (beanNames .length > 1 ) {
8685 this .logger .warn (LogMessage .format ("Found %s UserDetailsService beans, with names %s. "
8786 + "Global Authentication Manager will not use a UserDetailsService for username/password login. "
88- + "Consider publishing a single UserDetailsService bean." , userDetailsServices . size () ,
89- beanNames ));
87+ + "Consider publishing a single UserDetailsService bean." , beanNames . length ,
88+ Arrays . toString ( beanNames ) ));
9089 return ;
9190 }
92- UserDetailsService userDetailsService = userDetailsServices . get ( 0 ). getBean ();
93- String userDetailsServiceBeanName = userDetailsServices . get ( 0 ). getName ( );
91+ UserDetailsService userDetailsService = InitializeUserDetailsBeanManagerConfigurer . this . context
92+ . getBean ( beanNames [ 0 ], UserDetailsService . class );
9493 PasswordEncoder passwordEncoder = getBeanOrNull (PasswordEncoder .class );
9594 UserDetailsPasswordService passwordManager = getBeanOrNull (UserDetailsPasswordService .class );
9695 CompromisedPasswordChecker passwordChecker = getBeanOrNull (CompromisedPasswordChecker .class );
@@ -111,8 +110,7 @@ else if (userDetailsServices.size() > 1) {
111110 provider .afterPropertiesSet ();
112111 auth .authenticationProvider (provider );
113112 this .logger .info (LogMessage .format (
114- "Global AuthenticationManager configured with UserDetailsService bean with name %s" ,
115- userDetailsServiceBeanName ));
113+ "Global AuthenticationManager configured with UserDetailsService bean with name %s" , beanNames [0 ]));
116114 }
117115
118116 /**
@@ -127,41 +125,6 @@ private <T> T getBeanOrNull(Class<T> type) {
127125 return InitializeUserDetailsBeanManagerConfigurer .this .context .getBean (beanNames [0 ], type );
128126 }
129127
130- /**
131- * @return a list of beans of the requested class, along with their names. If
132- * there are no registered beans of that type, the list is empty.
133- */
134- private <T > List <BeanWithName <T >> getBeansWithName (Class <T > type ) {
135- List <BeanWithName <T >> beanWithNames = new ArrayList <>();
136- String [] beanNames = InitializeUserDetailsBeanManagerConfigurer .this .context .getBeanNamesForType (type );
137- for (String beanName : beanNames ) {
138- T bean = InitializeUserDetailsBeanManagerConfigurer .this .context .getBean (beanName , type );
139- beanWithNames .add (new BeanWithName <T >(bean , beanName ));
140- }
141- return beanWithNames ;
142- }
143-
144- static class BeanWithName <T > {
145-
146- private final T bean ;
147-
148- private final String name ;
149-
150- BeanWithName (T bean , String name ) {
151- this .bean = bean ;
152- this .name = name ;
153- }
154-
155- T getBean () {
156- return this .bean ;
157- }
158-
159- String getName () {
160- return this .name ;
161- }
162-
163- }
164-
165128 }
166129
167130}
0 commit comments