【问题标题】:Decode the Bcrypt encoded password in Spring Security to deactivate user account在 Spring Security 中解码 Bcrypt 编码的密码以停用用户帐户
【发布时间】:2015-01-10 09:53:05
【问题描述】:

我正在使用 Spring Hibernate MVC 开发 Web 应用程序项目。我在 Spring 安全性中使用 Bcrypt 算法将编码密码存储在数据库中。

现在我想对编码的密码进行解码以停用使用帐户,其中我在用户停用帐户之前向用户提供电子邮件和密码以进行验证。我在获取解码密码时遇到问题。

任何人都可以帮助我摆脱它或任何替代解决方案满足我的要求吗?

【问题讨论】:

    标签: bcrypt jbcrypt


    【解决方案1】:
    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
        ...
    }
    

    【讨论】:

      【解决方案2】:

      使用以下代码解决问题:

      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");
      }
      

      【讨论】:

      • 只是为了清楚 - 这实际上并没有像问题所暗示的那样解码编码的密码。根据 Spring 文档,BCryptPasswordEncoder.matches() 验证从存储中获得的编码密码与提交的原始密码在编码后是否匹配。
      猜你喜欢
      • 2019-10-26
      • 2015-08-10
      • 2016-01-23
      • 2021-03-20
      • 2020-05-26
      • 2019-03-25
      • 1970-01-01
      • 2017-04-03
      • 2021-12-12
      相关资源
      最近更新 更多