Насколько я могу понять, когда вы хотите выполнить специальную проверку подлинности в Spring Security, вы можете либо реализовать пользовательский AuthenticationProvider
или пользовательский UserDetailsService
.
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
//.authenticationProvider(authProvider) // option 1
.userDetailsService(userDetailsService); // option 2
}
В AuthenticationProvider вы можете проверить имя пользователя и пароль и вернуть Authentication
своим пользовательским объектом.
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");
}
}
В UserDetailsService
вы получаете только имя пользователя и когда вы возвращаете пользовательские UserDeatails, фреймворк выполняет проверку пароля.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
CustomUserDetails user = new CustomUserDetails();
//add whatever you want to the custom user details object
return user;
}
Похоже, что обе могут давать похожие результаты. Итак, вопрос в чем разница? Когда пользователь один против другого?