新しいGrailsプロジェクトに登録機能を追加しました。それをテストするために、私は電子メールとパスワードを与えて登録しました。私はデータベースに保存する前に、パスワードをハッシュするためにbcryptアルゴリズムを使用しています。Bcryptは同じ入力に対して異なるハッシュを生成しますか?
ただし、登録時に入力した電子メールとパスワードでログインしようとすると、ログインに失敗します。私はアプリケーションをデバッグし、データベースから既にハッシュされたものと比較しようとすると、同じパスワードに対して生成されたハッシュが異なることを知り、ログインが失敗しています(Registration.findByEmailAndPassword(params.email、hashPassd) LoginController.groovyはヌルを返します。ここで
は、自分のドメインクラス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")
}
}
}
は、ここでパスワードをハッシュするbcryptのアルゴリズムを使用するようにGrailsを言って、私のConfig.groovyからの抜粋だとキーイングの回数:
grails.plugins.springsecurity.password.algorithm = 'bcrypt'
grails.plugins.springsecurity.password.bcrypt.logrounds = 16
ありがとうBurt。それは働いていました。本当にありがとう、セッションにユーザーを格納することについての感謝の意を表します。私はgrailsに新しく、あなたが提案をすることができれば本当に感謝しています。ベストプラクティスなどに役立つものや....あなたのブログのポストへのリンクかもしれません(私はそれを愛する人の軍隊です...) – adit