16
16
17
17
package org .springframework .security .config .annotation .authentication .configuration ;
18
18
19
- import java .util .ArrayList ;
20
- import java .util .List ;
19
+ import java .util .Arrays ;
21
20
22
21
import org .apache .commons .logging .Log ;
23
22
import org .apache .commons .logging .LogFactory ;
@@ -66,9 +65,10 @@ class InitializeUserDetailsManagerConfigurer extends GlobalAuthenticationConfigu
66
65
67
66
@ Override
68
67
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 );
70
70
if (auth .isConfigured ()) {
71
- if (! userDetailsServices . isEmpty () ) {
71
+ if (beanNames . length > 0 ) {
72
72
this .logger .warn ("Global AuthenticationManager configured with an AuthenticationProvider bean. "
73
73
+ "UserDetailsService beans will not be used for username/password login. "
74
74
+ "Consider removing the AuthenticationProvider bean. "
@@ -78,19 +78,18 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
78
78
return ;
79
79
}
80
80
81
- if (userDetailsServices . isEmpty () ) {
81
+ if (beanNames . length == 0 ) {
82
82
return ;
83
83
}
84
- else if (userDetailsServices .size () > 1 ) {
85
- List <String > beanNames = userDetailsServices .stream ().map (BeanWithName ::getName ).toList ();
84
+ else if (beanNames .length > 1 ) {
86
85
this .logger .warn (LogMessage .format ("Found %s UserDetailsService beans, with names %s. "
87
86
+ "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 ) ));
90
89
return ;
91
90
}
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 );
94
93
PasswordEncoder passwordEncoder = getBeanOrNull (PasswordEncoder .class );
95
94
UserDetailsPasswordService passwordManager = getBeanOrNull (UserDetailsPasswordService .class );
96
95
CompromisedPasswordChecker passwordChecker = getBeanOrNull (CompromisedPasswordChecker .class );
@@ -111,8 +110,7 @@ else if (userDetailsServices.size() > 1) {
111
110
provider .afterPropertiesSet ();
112
111
auth .authenticationProvider (provider );
113
112
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 ]));
116
114
}
117
115
118
116
/**
@@ -127,41 +125,6 @@ private <T> T getBeanOrNull(Class<T> type) {
127
125
return InitializeUserDetailsBeanManagerConfigurer .this .context .getBean (beanNames [0 ], type );
128
126
}
129
127
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
-
165
128
}
166
129
167
130
}
0 commit comments