|
| 1 | +package com.baeldung.loginextrafieldscustom; |
| 2 | + |
| 3 | +import org.springframework.security.authentication.BadCredentialsException; |
| 4 | +import org.springframework.security.authentication.InternalAuthenticationServiceException; |
| 5 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 6 | +import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; |
| 7 | +import org.springframework.security.core.AuthenticationException; |
| 8 | +import org.springframework.security.core.userdetails.UserDetails; |
| 9 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 10 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 11 | +import org.springframework.util.Assert; |
| 12 | + |
| 13 | +public class CustomUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { |
| 14 | + |
| 15 | + /** |
| 16 | + * The plaintext password used to perform |
| 17 | + * PasswordEncoder#matches(CharSequence, String)} on when the user is |
| 18 | + * not found to avoid SEC-2056. |
| 19 | + */ |
| 20 | + private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword"; |
| 21 | + |
| 22 | + private PasswordEncoder passwordEncoder; |
| 23 | + private CustomUserDetailsService userDetailsService; |
| 24 | + |
| 25 | + /** |
| 26 | + * The password used to perform |
| 27 | + * {@link PasswordEncoder#matches(CharSequence, String)} on when the user is |
| 28 | + * not found to avoid SEC-2056. This is necessary, because some |
| 29 | + * {@link PasswordEncoder} implementations will short circuit if the password is not |
| 30 | + * in a valid format. |
| 31 | + */ |
| 32 | + private String userNotFoundEncodedPassword; |
| 33 | + |
| 34 | + public CustomUserDetailsAuthenticationProvider(PasswordEncoder passwordEncoder, CustomUserDetailsService userDetailsService) { |
| 35 | + this.passwordEncoder = passwordEncoder; |
| 36 | + this.userDetailsService = userDetailsService; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) |
| 41 | + throws AuthenticationException { |
| 42 | + |
| 43 | + if (authentication.getCredentials() == null) { |
| 44 | + logger.debug("Authentication failed: no credentials provided"); |
| 45 | + throw new BadCredentialsException( |
| 46 | + messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); |
| 47 | + } |
| 48 | + |
| 49 | + String presentedPassword = authentication.getCredentials() |
| 50 | + .toString(); |
| 51 | + |
| 52 | + if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) { |
| 53 | + logger.debug("Authentication failed: password does not match stored value"); |
| 54 | + throw new BadCredentialsException( |
| 55 | + messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void doAfterPropertiesSet() throws Exception { |
| 61 | + Assert.notNull(this.userDetailsService, "A UserDetailsService must be set"); |
| 62 | + this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) |
| 67 | + throws AuthenticationException { |
| 68 | + CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication; |
| 69 | + UserDetails loadedUser; |
| 70 | + |
| 71 | + try { |
| 72 | + loadedUser = this.userDetailsService.loadUserByUsernameAndDomain(auth.getPrincipal() |
| 73 | + .toString(), auth.getDomain()); |
| 74 | + } catch (UsernameNotFoundException notFound) { |
| 75 | + if (authentication.getCredentials() != null) { |
| 76 | + String presentedPassword = authentication.getCredentials() |
| 77 | + .toString(); |
| 78 | + passwordEncoder.matches(presentedPassword, userNotFoundEncodedPassword); |
| 79 | + } |
| 80 | + throw notFound; |
| 81 | + } catch (Exception repositoryProblem) { |
| 82 | + throw new InternalAuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem); |
| 83 | + } |
| 84 | + |
| 85 | + if (loadedUser == null) { |
| 86 | + throw new InternalAuthenticationServiceException("UserDetailsService returned null, " |
| 87 | + + "which is an interface contract violation"); |
| 88 | + } |
| 89 | + return loadedUser; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments