Skip to content

Commit

Permalink
Add new DaoAuthenticationProvider constructor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
psvo authored and marcusdacoregio committed Apr 4, 2023
1 parent 05675e8 commit 44c4a4a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
private UserDetailsPasswordService userDetailsPasswordService;

public DaoAuthenticationProvider() {
setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
this(PasswordEncoderFactories.createDelegatingPasswordEncoder());
}

/**
* Creates a new instance using the provided {@link PasswordEncoder}
* @param passwordEncoder the {@link PasswordEncoder} to use. Cannot be null.
* @since 6.0.3
*/
public DaoAuthenticationProvider(PasswordEncoder passwordEncoder) {
setPasswordEncoder(passwordEncoder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ public void testUserNotFoundDefaultEncoder() {
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
}

@Test
public void constructWhenPasswordEncoderProvidedThenSets() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(
NoOpPasswordEncoder.getInstance());
assertThat(daoAuthenticationProvider.getPasswordEncoder()).isSameAs(NoOpPasswordEncoder.getInstance());
}

/**
* This is an explicit test for SEC-2056. It is intentionally ignored since this test
* is not deterministic and {@link #testUserNotFoundEncodesPassword()} ensures that
Expand Down

0 comments on commit 44c4a4a

Please sign in to comment.