Answer the question
In order to leave comments, you need to log in
Why can't they match when comparing 2 hashes of BCryptPasswordEncoder?
Hello!
Quite a strange thing... Everything used to work, but now it doesn't work correctly. There is a simple signup form and I am using BCryptPasswordEncoder for the password.
Accordingly, here is the minimum code to work:
Spring Security Config
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("SELECT email, password, active FROM users WHERE email=?")
.authoritiesByUsernameQuery("SELECT email, role FROM users WHERE email=?");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
PasswordEncoder passwordEncoder;
@PostMapping("/profile")
public String updateProfile(
@RequestParam String password,
@RequestParam String email,
@RequestParam(value = "userRole",required=false) String userRole,
@AuthenticationPrincipal UserDetails currentUser
) {
User user = (User) userService.findUserByEmail(currentUser.getUsername());
user.setEmail(email);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
if(!encoder.matches(password, user.getPassword())) {
user.setPassword(passwordEncoder.encode(password));
}
user.setRole(Role.valueOf(userRole)); //todo обновить процесс смены прав пользователя
userService.updateUser(user.getUser_id(), user);
return "redirect:/profile?updated";
}
$2a$10$Qx1zgFOWqlTkpSUI0pb5CuQzFnwIq3wxNyn.tjk8NT6kmrZAN3Lv.
$2a$10$Qx1zgFOWqlTkpSUI0pb5CuQzFnwIq3wxNyn.tjk8NT6kmrZAN3Lv.
$2a$10$Qx1zgFOWqlTkpSUI0pb5CuQzFnwIq3wxNyn.tjk8NT6kmrZAN3Lv.
Answer the question
In order to leave comments, you need to log in
if(!encoder.matches(password, user.getPassword())) {
user.setPassword(passwordEncoder.encode(password));
}
if(!encoder.matches(passwordEncoder.encode(password), user.getPassword())) {
user.setPassword(passwordEncoder.encode(password));
}
I had a problem that when changing the role of the user, the password itself also changed. This helped
if (!user.getPassword().equals(userService.getById(user.getId()).getPassword())){
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question