16
16
17
17
package org .springframework .security .authentication .dao ;
18
18
19
+ import java .util .function .Supplier ;
20
+
19
21
import org .springframework .security .authentication .AuthenticationProvider ;
20
22
import org .springframework .security .authentication .BadCredentialsException ;
21
23
import org .springframework .security .authentication .InternalAuthenticationServiceException ;
31
33
import org .springframework .security .crypto .factory .PasswordEncoderFactories ;
32
34
import org .springframework .security .crypto .password .PasswordEncoder ;
33
35
import org .springframework .util .Assert ;
36
+ import org .springframework .util .function .SingletonSupplier ;
34
37
35
38
/**
36
39
* An {@link AuthenticationProvider} implementation that retrieves user details from a
@@ -48,7 +51,8 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
48
51
*/
49
52
private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword" ;
50
53
51
- private PasswordEncoder passwordEncoder ;
54
+ private Supplier <PasswordEncoder > passwordEncoder = SingletonSupplier
55
+ .of (PasswordEncoderFactories ::createDelegatingPasswordEncoder );
52
56
53
57
/**
54
58
* The password used to perform {@link PasswordEncoder#matches(CharSequence, String)}
@@ -64,15 +68,25 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
64
68
65
69
private CompromisedPasswordChecker compromisedPasswordChecker ;
66
70
71
+ /**
72
+ * @deprecated Please provide the {@link UserDetailsService} in the constructor
73
+ */
74
+ @ Deprecated
67
75
public DaoAuthenticationProvider () {
68
- this (PasswordEncoderFactories .createDelegatingPasswordEncoder ());
76
+ }
77
+
78
+ public DaoAuthenticationProvider (UserDetailsService userDetailsService ) {
79
+ setUserDetailsService (userDetailsService );
69
80
}
70
81
71
82
/**
72
83
* Creates a new instance using the provided {@link PasswordEncoder}
73
84
* @param passwordEncoder the {@link PasswordEncoder} to use. Cannot be null.
74
85
* @since 6.0.3
86
+ * @deprecated Please provide the {@link UserDetailsService} in the constructor
87
+ * followed by {@link #setPasswordEncoder(PasswordEncoder)} instead
75
88
*/
89
+ @ Deprecated
76
90
public DaoAuthenticationProvider (PasswordEncoder passwordEncoder ) {
77
91
setPasswordEncoder (passwordEncoder );
78
92
}
@@ -87,7 +101,7 @@ protected void additionalAuthenticationChecks(UserDetails userDetails,
87
101
.getMessage ("AbstractUserDetailsAuthenticationProvider.badCredentials" , "Bad credentials" ));
88
102
}
89
103
String presentedPassword = authentication .getCredentials ().toString ();
90
- if (!this .passwordEncoder .matches (presentedPassword , userDetails .getPassword ())) {
104
+ if (!this .passwordEncoder .get (). matches (presentedPassword , userDetails .getPassword ())) {
91
105
this .logger .debug ("Failed to authenticate since password does not match stored value" );
92
106
throw new BadCredentialsException (this .messages
93
107
.getMessage ("AbstractUserDetailsAuthenticationProvider.badCredentials" , "Bad credentials" ));
@@ -133,24 +147,24 @@ protected Authentication createSuccessAuthentication(Object principal, Authentic
133
147
throw new CompromisedPasswordException ("The provided password is compromised, please change your password" );
134
148
}
135
149
boolean upgradeEncoding = this .userDetailsPasswordService != null
136
- && this .passwordEncoder .upgradeEncoding (user .getPassword ());
150
+ && this .passwordEncoder .get (). upgradeEncoding (user .getPassword ());
137
151
if (upgradeEncoding ) {
138
- String newPassword = this .passwordEncoder .encode (presentedPassword );
152
+ String newPassword = this .passwordEncoder .get (). encode (presentedPassword );
139
153
user = this .userDetailsPasswordService .updatePassword (user , newPassword );
140
154
}
141
155
return super .createSuccessAuthentication (principal , authentication , user );
142
156
}
143
157
144
158
private void prepareTimingAttackProtection () {
145
159
if (this .userNotFoundEncodedPassword == null ) {
146
- this .userNotFoundEncodedPassword = this .passwordEncoder .encode (USER_NOT_FOUND_PASSWORD );
160
+ this .userNotFoundEncodedPassword = this .passwordEncoder .get (). encode (USER_NOT_FOUND_PASSWORD );
147
161
}
148
162
}
149
163
150
164
private void mitigateAgainstTimingAttack (UsernamePasswordAuthenticationToken authentication ) {
151
165
if (authentication .getCredentials () != null ) {
152
166
String presentedPassword = authentication .getCredentials ().toString ();
153
- this .passwordEncoder .matches (presentedPassword , this .userNotFoundEncodedPassword );
167
+ this .passwordEncoder .get (). matches (presentedPassword , this .userNotFoundEncodedPassword );
154
168
}
155
169
}
156
170
@@ -163,14 +177,19 @@ private void mitigateAgainstTimingAttack(UsernamePasswordAuthenticationToken aut
163
177
*/
164
178
public void setPasswordEncoder (PasswordEncoder passwordEncoder ) {
165
179
Assert .notNull (passwordEncoder , "passwordEncoder cannot be null" );
166
- this .passwordEncoder = passwordEncoder ;
180
+ this .passwordEncoder = () -> passwordEncoder ;
167
181
this .userNotFoundEncodedPassword = null ;
168
182
}
169
183
170
184
protected PasswordEncoder getPasswordEncoder () {
171
- return this .passwordEncoder ;
185
+ return this .passwordEncoder . get () ;
172
186
}
173
187
188
+ /**
189
+ * @param userDetailsService
190
+ * @deprecated Please provide the {@link UserDetailsService} in the constructor
191
+ */
192
+ @ Deprecated
174
193
public void setUserDetailsService (UserDetailsService userDetailsService ) {
175
194
this .userDetailsService = userDetailsService ;
176
195
}
0 commit comments