小编adi*_*dit的帖子

Bcrypt为同一输入生成不同的哈希值?

我刚刚为我的新grails项目添加了注册功能.为了测试它,我通过发送电子邮件和密码进行了注册.我使用bcrypt算法对密码进行哈希处理,然后将其保存到数据库中.

但是,当我尝试使用注册时提供的相同电子邮件和密码登录时,登录失败.我调试了应用程序,发现当我尝试与数据库中已经散列的哈希值进行比较时,为同一密码生成的哈希是不同的,因此登录失败(LoginController中的Registration.findByEmailAndPassword(params.email,hashPassd)) .groovy返回null).

这是我的域类Registration.groovy:

class Registration {

   transient springSecurityService

   String fullName
   String password
   String email

   static constraints = {
      fullName(blank:false)
      password(blank:false, password:true)
      email(blank:false, email:true, unique:true)
   }

   def beforeInsert = {
      encodePassword()
   }

   protected void encodePassword() {
      password = springSecurityService.encodePassword(password)
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的LoginController.groovy:

class LoginController {

   /**
    * Dependency injection for the springSecurityService.
    */
   def springSecurityService

   def index = {
      if (springSecurityService.isLoggedIn()) {
         render(view: "../homepage")
      }
      else {
         render(view: "../index")
      }
   }

   /**
    * Show the login page.
    */ …
Run Code Online (Sandbox Code Playgroud)

grails bcrypt

36
推荐指数
2
解决办法
2万
查看次数

标签 统计

bcrypt ×1

grails ×1