As far as I can understand when you want custom authentication in Spring Security you can either implement a custom AuthenticationProvider
or custom UserDetailsService
.
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
//.authenticationProvider(authProvider) // option 1
.userDetailsService(userDetailsService); // option 2
}
In the AuthenticationProvider you can check the username and password and return Authentication
with your custom object in it.
public Authentication authenticate(Authentication authentication){
if (checkUsernameAndPassword(authentication)) {
CustomUserDetails userDetails = new CustomUserDetails();
//add whatever you want to the custom user details object
return new UsernamePasswordAuthenticationToken(userDetails, password, grantedAuths);
} else {
throw new BadCredentialsException("Unable to auth against third party systems");
}
}
In the UserDetailsService
you get only the username and when you return the custom UserDeatails, the framework performs a check on the password.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
CustomUserDetails user = new CustomUserDetails();
//add whatever you want to the custom user details object
return user;
}
Looks like both can produce similar results. So the question is what is the difference? When to user one vs the other?