我刚刚为我的新Grails项目添加了注册功能。为了测试它,我通过发送邮件和密码进行了注册。在将密码保存到数据库之前,我正在使用bcrypt算法对密码进行散列处理。Bcrypt为相同的输入生成不同的哈希值?
但是,当我尝试使用登录时输入的电子邮件和密码进行登录时,登录失败。我调试了应用程序,发现为相同密码生成的散列不同于我尝试与已经散列的数据库进行比较,因此登录失败(Registration.findByEmailAndPassword(params.email,hashPassd)in LoginController.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)
}
}
这里是我的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.
*/
def handleLogin = {
if (springSecurityService.isLoggedIn()) {
render(view: "../homepage")
return
}
def hashPassd = springSecurityService.encodePassword(params.password)
// Find the username
def user = Registration.findByEmailAndPassword(params.email,hashPassd)
if (!user) {
flash.message = "User not found for email: ${params.email}"
render(view: "../index")
return
} else {
session.user = user
render(view: "../homepage")
}
}
}
下面是从我的Config.groovy告诉Grails的使用bcrypt算法的密码散列片段和轮回密钥的数量:
grails.plugins.springsecurity.password.algorithm = 'bcrypt'
grails.plugins.springsecurity.password.bcrypt.logrounds = 16
感谢Burt。它的工作,并非常感谢关于在会话中存储用户的建议。我是grails的新手,并且正在使用它来开发社交如果你能提出建议,我会很感激在最佳做法等或任何有帮助的....可能是一个链接到您的博客(一个solipsists军队.....我喜欢它) – adit