我正在 Spring Hibernate MVC 中开发 Web 应用程序项目。我在 Spring 安全性中使用 Bcrypt 算法将编码密码存储在数据库中。
现在,我想对该编码密码进行解码以停用使用帐户,在该帐户中,我要在用户停用帐户之前向用户提供电子邮件和密码以进行验证。我在获取解码密码时遇到问题。
任何人都可以帮助我摆脱它或满足我的要求的任何替代解决方案吗?
使用以下代码解决了该问题:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
encoder.matches(password, user.getPassword());
password- 来自表单(JSP)
user.getPassword()- 来自数据库
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
if(email.equalsIgnoreCase(user.getEmail()) && encoder.matches(password, user.getPassword())) {
userService.deactivateUserByID(user.getId());
redirectAttributes.addFlashAttribute("successmsg", "Your account has been deactivated successfully.");
model.setViewName("redirect:/logout");
}else{
redirectAttributes.addFlashAttribute("errormsg", "Email or Password is incorrect");
model.setViewName("redirect:/app/profile/deactivate");
}
BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder();
boolean isPasswordMatches = bcrypt.matches(userenteredpasswordWithotEncryoted, encryptedPasswordFromDb);
例子:
boolean isPasswordMatches = bcrypt.matches(
"Truck123",
"$2a$10$kcVH3Uy86nJgQtYqAFffZORT9wbNMuNtqytcUZQRX51dx6IfSFEd."
);
if (isPasswordMatches) { // correct password
...
} else { // Wrong Password
...
}