I am using spring-boot-starter-security
. I configured my WebSecurityConfigation
to use DaoAuthenticationProvider
provider and BCryptPasswordEncoder
for authentication. Also the UserDetailsService
implementation returns a User
object with the password
field set to the actual hash.
It seems to work fine. However i noticed that i could successfully authenticate with either the password or the hash.
For example the password itself is a generated UUID 51a80a6a-8618-4583-98d2-d77d103a62c6
which was encoded to $2a$10$u4OSZf7B9yJvQ5UYNNpy7O4f3g0gfUMl2Xmm3h282W.3emSN3WqxO
.
Full web security configuration:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DemoUserDetailsService userDetailsService;
@Autowired
private DaoAuthenticationProvider authenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.userDetailsService(userDetailsService);
auth.inMemoryAuthentication().withUser("user").password("password").roles("SUPER", "BASIC");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("BASIC").and().httpBasic();
http.csrf().disable();
}
}
@Service
public class DemoUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserDAO userDAO = userRepository.findByEmailAndActivated(email);
if (userDAO == null) {
throw new UsernameNotFoundException(String.format("Email %s not found", email));
}
return new User(email, userDAO.getPasswordHash(), getGrantedAuthorities(email));
}
private Collection<? extends GrantedAuthority> getGrantedAuthorities(String email) {
return asList(() -> "ROLE_BASIC");
}
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder);
return authenticationProvider;
}
Why am i able to authenticate with both strings? Am i doing something wrong, or is this expected or some configuration? I was unable to find anything in docs.