新しい grails プロジェクトに登録機能を追加しました。テストのために、メールアドレスとパスワードを入力して登録しました。データベースに保存する前に、パスワードをハッシュするために bcrypt アルゴリズムを使用しています。
ただし、登録時に指定したのと同じメールアドレスとパスワードでログインしようとすると、ログインに失敗します。私はアプリケーションをデバッグし、データベースから既にハッシュされたものと比較しようとすると、同じパスワードに対して生成されたハッシュが異なることがわかりました。 .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 からのスニペットで、bcrypt アルゴリズムを使用してパスワードとキーイングのラウンド数をハッシュするように grails に指示しています。
grails.plugins.springsecurity.password.algorithm = 'bcrypt'
grails.plugins.springsecurity.password.bcrypt.logrounds = 16