Skip to content

Commit 44c4a4a

Browse files
psvomarcusdacoregio
authored andcommitted
Add new DaoAuthenticationProvider constructor
Add a new constructor to the DaoAuthenticationProvider, which allows providing a custom PasswordEncoder to prevent instantiation of the default delegating PasswordEncoder in the default constructor. This provides a way to instantiate the DaoAuthenticationProvider on JDKs where the default delegating PasswordEncoder cannot be instantiated due to limited JCE providers for compliance reasons (e.g., FIPS). Closes gh-12874
1 parent 05675e8 commit 44c4a4a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core/src/main/java/org/springframework/security/authentication/dao/DaoAuthenticationProvider.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
6161
private UserDetailsPasswordService userDetailsPasswordService;
6262

6363
public DaoAuthenticationProvider() {
64-
setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
64+
this(PasswordEncoderFactories.createDelegatingPasswordEncoder());
65+
}
66+
67+
/**
68+
* Creates a new instance using the provided {@link PasswordEncoder}
69+
* @param passwordEncoder the {@link PasswordEncoder} to use. Cannot be null.
70+
* @since 6.0.3
71+
*/
72+
public DaoAuthenticationProvider(PasswordEncoder passwordEncoder) {
73+
setPasswordEncoder(passwordEncoder);
6574
}
6675

6776
@Override

core/src/test/java/org/springframework/security/authentication/dao/DaoAuthenticationProviderTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ public void testUserNotFoundDefaultEncoder() {
441441
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
442442
}
443443

444+
@Test
445+
public void constructWhenPasswordEncoderProvidedThenSets() {
446+
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(
447+
NoOpPasswordEncoder.getInstance());
448+
assertThat(daoAuthenticationProvider.getPasswordEncoder()).isSameAs(NoOpPasswordEncoder.getInstance());
449+
}
450+
444451
/**
445452
* This is an explicit test for SEC-2056. It is intentionally ignored since this test
446453
* is not deterministic and {@link #testUserNotFoundEncodesPassword()} ensures that

0 commit comments

Comments
 (0)