在我的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