使用Grails的spring安全插件创建用户
我正在Grails应用程序中使用spring security核心插件。我成功地安装了插件,并在引导程序中提供了一个默认用户,它工作正常。但是,我想提供在应用程序中创建新用户(注册)的选项。实现这一目标的最佳方式是什么?我在UserController中尝试了以下代码:使用Grails的spring安全插件创建用户,grails,spring-security,Grails,Spring Security,我正在Grails应用程序中使用spring security核心插件。我成功地安装了插件,并在引导程序中提供了一个默认用户,它工作正常。但是,我想提供在应用程序中创建新用户(注册)的选项。实现这一目标的最佳方式是什么?我在UserController中尝试了以下代码: def save = { def userRole = SecRole.findByAuthority("ROLE_USER")?: new SecRole(authority:"ROLE_USER").save(fa
def save = {
def userRole = SecRole.findByAuthority("ROLE_USER")?: new SecRole(authority:"ROLE_USER").save(failOnError:true)
def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority:"ROLE_ADMIN").save(failOnError:true)
def newUser = new User(
username: params.username,
password: springSecurityService.encodePassword(params.password),
enabled: true)
SecUserSecRole.create newUser, userRole
}
但是它不工作,抛出java.lang.NullPointerException。
有什么想法吗?
提前感谢。您需要对用户调用
save()
。我试图加载不存在的角色。我用上面的注释对它进行了测试(保存用户,在修复了我的错误后不保存,两个版本都工作了)。所以SecUserSecRole.create对我来说就足够了 谢谢!我完全忘了保存