在我的Grails 2.5.1項目中,我使用的是彈簧安全核心:2.0 RC5插件,收到此錯誤:編碼與Spring Security的Grails的插件密碼
Error initializing the application: Cannot invoke method encodePassword() on null object
這裏是我的BootStrap:
class BootStrap {
def init = { servletContext ->
def springSecurityService
def userRole = SecurityRole.findByAuthority("ROLE_USER") ?: new SecurityRole(authority:"ROLE_USER").save(flush:true)
def adminRole = SecurityRole.findByAuthority("ROLE_ADMIN") ?: new SecurityRole(authority:"ROLE_ADMIN").save(flush:true)
def user = new User(username:"user" ,password:springSecurityService.encodePassword("123"),enable:true).save(flush:true)
def admin = new User(username:"admin" ,password:springSecurityService.encodePassword("1234"),enable:true).save(flush:true)
SecurityUserSecurityRole.create(user, userRole)
SecurityUserSecurityRole.create(admin, adminRole)
}
def destroy = {
}
}
我錯過了什麼?
感謝您的詳細回答,您現在明確告訴我,但我正面臨一件新事物,除了** userRole **和** adminRole **以外,沒有任何數據存儲在數據庫中,用戶沒有存儲!你有看到代碼中的任何東西嗎? – Sherif
這可能是一個驗證錯誤 - 您是否向User類添加了新的必需屬性?所有屬性在默認情況下都是'nullable:false'。我幾乎從不使用'failOnError',因爲拋出一個驗證異常的代價很大,但是當你有硬編碼的值時,你希望這裏有效(另一種情況是測試中的數據),我會用它來使這個問題很明顯,並且要迅速失敗。將兩個用戶的'save()'改爲'save(failOnError:true)',它應該顯示缺少的內容。 –
雅這是一些驗證錯誤,真的很感謝您的支持,順便說一句,你有任何有用的和有用的資源,爲Grails給我很好的推動,因爲我是有經驗的Java開發人員,但我是新來的Grails,我需要完成一個小項目。 – Sherif