我正在使用spring-boot-starter-security
. 我将我的配置WebSecurityConfigation
为使用DaoAuthenticationProvider
提供程序和BCryptPasswordEncoder
身份验证。该UserDetailsService
实现还返回一个字段设置为实际哈希的User
对象。password
它似乎工作正常。但是我注意到我可以使用密码或哈希成功进行身份验证。
例如,密码本身是一个生成的 UUID 51a80a6a-8618-4583-98d2-d77d103a62c6
,它被编码为$2a$10$u4OSZf7B9yJvQ5UYNNpy7O4f3g0gfUMl2Xmm3h282W.3emSN3WqxO
.
完整的网络安全配置:
@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;
}
为什么我能够使用两个字符串进行身份验证?我做错了什么,或者这是预期的还是一些配置?我在文档中找不到任何内容。