如何在Spring Boot中设置用户在使用bcrypt进行用户注册后的自动登录,获取错误凭据
我有一个问题,关于如何在用户注册我的Spring Boot应用程序后自动登录用户。使用Bcrypt将用户密码保存到MySQL数据库中 这是一种我必须正确创建和保存新用户的方法,它似乎工作得很好:如何在Spring Boot中设置用户在使用bcrypt进行用户注册后的自动登录,获取错误凭据,spring,spring-mvc,spring-boot,spring-security,bcrypt,Spring,Spring Mvc,Spring Boot,Spring Security,Bcrypt,我有一个问题,关于如何在用户注册我的Spring Boot应用程序后自动登录用户。使用Bcrypt将用户密码保存到MySQL数据库中 这是一种我必须正确创建和保存新用户的方法,它似乎工作得很好: @Autowired private UserRepository userRepository; @Autowired private BCryptPasswordEncoder bCryptPasswordEncoder; public User save(User user, Role rol
@Autowired
private UserRepository userRepository;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
public User save(User user, Role role) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user.setRoles(new HashSet<>(Arrays.asList(role)));
userRepository.save(user);
return user;
}
现在,当它到达终点时:
authenticationManager.authenticate(usernamePasswordAuthenticationToken);
它总是抱怨:
00:36:47.635 [http-nio-5000-exec-10] DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
org.springframework.security.authentication.BadCredentialsException: Bad credentials
我觉得这很奇怪。在我的Web安全配置适配器中,我设置了以下各项:
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userServiceManager).passwordEncoder(bCryptPasswordEncoder());
}
如果我错过了什么,有人能告诉我吗?
谢谢您是否也在
loadUserByUsername(username)中获取密码代码>并且它不是Spring Boot 4!最新的Spring启动版本是2.0的里程碑版本,所以应该是Spring Security 4!在调试模式下,它是否打印了此行的任何值userDetails.getAuthorities()
?Hi@ataurRahmanMunna,是的,userDetails.getAuthorities()确实返回集合中的一项,即角色表中的“用户”角色。Hi@MohamedSanaulla,是,loadUserByUsername返回的UserDetails对象确实包含BCrypt加密密码在我的示例中,我创建了一个daoAuthProvider@Bean public DAOAAuthenticationProvider authProvider(){DAOAAuthenticationProvider authProvider=new DAOAAuthenticationProvider();authProvider.setPasswordEncoder(passwordEncoder());authProvider.setUserDetailsService(userDetailsService());return authProvider;}
对于authBuilder设置,auth.authenticationProvider(authProvider())…。
您是否也在loadUserByUsername(username)中获取密码代码>并且它不是Spring Boot 4!最新的Spring启动版本是2.0的里程碑版本,所以应该是Spring Security 4!在调试模式下,它是否打印了此行的任何值userDetails.getAuthorities()
?Hi@ataurRahmanMunna,是的,userDetails.getAuthorities()确实返回集合中的一项,即角色表中的“用户”角色。Hi@MohamedSanaulla,是,loadUserByUsername返回的UserDetails对象确实包含BCrypt加密密码在我的示例中,我创建了一个daoAuthProvider@Bean public DAOAAuthenticationProvider authProvider(){DAOAAuthenticationProvider authProvider=new DAOAAuthenticationProvider();authProvider.setPasswordEncoder(passwordEncoder());authProvider.setUserDetailsService(userDetailsService());返回authProvider;}
对于authBuilder设置auth.authenticationProvider(authProvider())….
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userServiceManager).passwordEncoder(bCryptPasswordEncoder());
}