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 ;
@@ -67,9 +66,10 @@ class InitializeUserDetailsManagerConfigurer extends GlobalAuthenticationConfigu
67
66
68
67
@ Override
69
68
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 );
71
71
if (auth .isConfigured ()) {
72
- if (! userDetailsServices . isEmpty () ) {
72
+ if (beanNames . length > 0 ) {
73
73
this .logger .warn ("Global AuthenticationManager configured with an AuthenticationProvider bean. "
74
74
+ "UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. "
75
75
+ "Consider removing the AuthenticationProvider bean. "
@@ -80,19 +80,18 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
80
80
return ;
81
81
}
82
82
83
- if (userDetailsServices . isEmpty () ) {
83
+ if (beanNames . length == 0 ) {
84
84
return ;
85
85
}
86
- else if (userDetailsServices .size () > 1 ) {
87
- List <String > beanNames = userDetailsServices .stream ().map (BeanWithName ::getName ).toList ();
86
+ else if (beanNames .length > 1 ) {
88
87
this .logger .warn (LogMessage .format ("Found %s UserDetailsService beans, with names %s. "
89
88
+ "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 ) ));
92
91
return ;
93
92
}
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 );
96
95
PasswordEncoder passwordEncoder = getBeanOrNull (PasswordEncoder .class );
97
96
UserDetailsPasswordService passwordManager = getBeanOrNull (UserDetailsPasswordService .class );
98
97
CompromisedPasswordChecker passwordChecker = getBeanOrNull (CompromisedPasswordChecker .class );
@@ -113,8 +112,7 @@ else if (userDetailsServices.size() > 1) {
113
112
provider .afterPropertiesSet ();
114
113
auth .authenticationProvider (provider );
115
114
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 ]));
118
116
}
119
117
120
118
/**
@@ -125,41 +123,6 @@ private <T> T getBeanOrNull(Class<T> type) {
125
123
return InitializeUserDetailsBeanManagerConfigurer .this .context .getBeanProvider (type ).getIfUnique ();
126
124
}
127
125
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
-
163
126
}
164
127
165
128
}
0 commit comments