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 ;
@@ -67,9 +66,10 @@ class InitializeUserDetailsManagerConfigurer extends GlobalAuthenticationConfigu
6766
6867 @ Override
6968 public void configure (AuthenticationManagerBuilder auth ) throws Exception {
70- List <BeanWithName <UserDetailsService >> userDetailsServices = getBeansWithName (UserDetailsService .class );
69+ String [] beanNames = InitializeUserDetailsBeanManagerConfigurer .this .context
70+ .getBeanNamesForType (UserDetailsService .class );
7171 if (auth .isConfigured ()) {
72- if (! userDetailsServices . isEmpty () ) {
72+ if (beanNames . length > 0 ) {
7373 this .logger .warn ("Global AuthenticationManager configured with an AuthenticationProvider bean. "
7474 + "UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. "
7575 + "Consider removing the AuthenticationProvider bean. "
@@ -80,19 +80,18 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
8080 return ;
8181 }
8282
83- if (userDetailsServices . isEmpty () ) {
83+ if (beanNames . length == 0 ) {
8484 return ;
8585 }
86- else if (userDetailsServices .size () > 1 ) {
87- List <String > beanNames = userDetailsServices .stream ().map (BeanWithName ::getName ).toList ();
86+ else if (beanNames .length > 1 ) {
8887 this .logger .warn (LogMessage .format ("Found %s UserDetailsService beans, with names %s. "
8988 + "Global Authentication Manager will not use a UserDetailsService for username/password login. "
90- + "Consider publishing a single UserDetailsService bean." , userDetailsServices . size () ,
91- beanNames ));
89+ + "Consider publishing a single UserDetailsService bean." , beanNames . length ,
90+ Arrays . toString ( beanNames ) ));
9291 return ;
9392 }
94- UserDetailsService userDetailsService = userDetailsServices . get ( 0 ). getBean ();
95- String userDetailsServiceBeanName = userDetailsServices . get ( 0 ). getName ( );
93+ UserDetailsService userDetailsService = InitializeUserDetailsBeanManagerConfigurer . this . context
94+ . getBean ( beanNames [ 0 ], UserDetailsService . class );
9695 PasswordEncoder passwordEncoder = getBeanOrNull (PasswordEncoder .class );
9796 UserDetailsPasswordService passwordManager = getBeanOrNull (UserDetailsPasswordService .class );
9897 CompromisedPasswordChecker passwordChecker = getBeanOrNull (CompromisedPasswordChecker .class );
@@ -113,8 +112,7 @@ else if (userDetailsServices.size() > 1) {
113112 provider .afterPropertiesSet ();
114113 auth .authenticationProvider (provider );
115114 this .logger .info (LogMessage .format (
116- "Global AuthenticationManager configured with UserDetailsService bean with name %s" ,
117- userDetailsServiceBeanName ));
115+ "Global AuthenticationManager configured with UserDetailsService bean with name %s" , beanNames [0 ]));
118116 }
119117
120118 /**
@@ -125,41 +123,6 @@ private <T> T getBeanOrNull(Class<T> type) {
125123 return InitializeUserDetailsBeanManagerConfigurer .this .context .getBeanProvider (type ).getIfUnique ();
126124 }
127125
128- /**
129- * @return a list of beans of the requested class, along with their names. If
130- * there are no registered beans of that type, the list is empty.
131- */
132- private <T > List <BeanWithName <T >> getBeansWithName (Class <T > type ) {
133- List <BeanWithName <T >> beanWithNames = new ArrayList <>();
134- String [] beanNames = InitializeUserDetailsBeanManagerConfigurer .this .context .getBeanNamesForType (type );
135- for (String beanName : beanNames ) {
136- T bean = InitializeUserDetailsBeanManagerConfigurer .this .context .getBean (beanName , type );
137- beanWithNames .add (new BeanWithName <>(bean , beanName ));
138- }
139- return beanWithNames ;
140- }
141-
142- static class BeanWithName <T > {
143-
144- private final T bean ;
145-
146- private final String name ;
147-
148- BeanWithName (T bean , String name ) {
149- this .bean = bean ;
150- this .name = name ;
151- }
152-
153- T getBean () {
154- return this .bean ;
155- }
156-
157- String getName () {
158- return this .name ;
159- }
160-
161- }
162-
163126 }
164127
165128}
0 commit comments