[英]spring security core grails not logging in
安裝Spring Security Core作為插件,然后快速入門
這是我的用戶域類
package auth
class User {
def springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static mapping = {
// password is a keyword in some sql dialects, so quote with backticks
// password is stored as 44-char base64 hashed value
password column: '`password`', length: 64
}
static constraints = {
username blank: false, size: 1..50, unique: true
password blank: false, size: 8..100
}
Set getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected encodePassword() {
password = springSecurityService.encodePassword(password, username)
}
}
我的boostrap.groovy是
class BootStrap {
def init = { servletContext ->
auth.User james = new auth.User(username: 'test', enabled: true, password: 'password')
james.save()
if (james.hasErrors())
{
println("he has errors")
}
println("we made it! ")
}
def destroy = {
}
}
但是,當我登錄時,它總是說:“抱歉,我們找不到具有該用戶名和密碼的用戶。” 有什么想法嗎?
這是因為您在編碼密碼時使用了鹽 。
password = springSecurityService.encodePassword(password, username)
我不知道加鹽,因此不能指導您太多。
但是,如果您在不加鹽的情況下對密碼進行編碼,則代碼可以正常工作, 只需在對密碼進行編碼時刪除用戶名 ,請嘗試執行此操作
password = springSecurityService.encodePassword(password)
希望這可以幫助。
如果您在BootStrap.groovy
創建用戶,請嘗試更改此設置:
def adminUser = User.findByUsername('admin') ?: new User(
username: 'admin',
password: springSecurityService.encodePassword('admin'),
enabled: true).save(failOnError: true)
對此:
def adminUser = User.findByUsername('admin') ?: new User(
username: 'admin',
password: 'admin',
enabled: true).save(failOnError: true)
問題是您使用了兩次編碼密碼,一次在域中,一次在構造函數的參數中。
您可以驗證用戶是否實際上已引導到數據庫中嗎?
如果是這樣,我遇到了類似的問題,Tomcat錯誤地緩存了一些數據。
這是我所做的:
在那之后,它運行良好。
讓我知道是否有幫助。
另外,自從我從頭開始構建Grails網站以來已經有一段時間了,但是我想我還記得一些在線說明存在問題。 SpringSecurity可能正在為您編碼密碼,所以當您進行密碼編碼時,它會被雙重編碼 。
嘗試刪除編碼密碼的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.