2012-09-24 106 views
0

我有一些问题,从我的一个控制器创建新用户。我正在试图向我的MongoDB用户集合添加一个新用户,像这样。当局被定义为域中的一组角色。grails弹簧安全创建用户

Role role = new Role(authority:"ROLE_USER") 
User user = new User(username:params.username,email:params.email,password:params.password,enabled:params.enabled, 
      accountExpired:params.accountExpired,accountLocked:params.accountLocked,passwordExpired:params.passwordExpired, 
      authorities:[role]) 

if (user.validate()) { 
     user.save(flush:true) 
     } else { 
     user.errors.allErrors.each { println it } 
     } 

完全相同的代码能够从引导成功创建用户,但是当我试图从一个简单的控制器,我得到这个错误做同样的事情:

2012-09-24 10:43:27,450 [http-8080-3] ERROR binding.GrailsDataBinder - Unable to auto-create type interface java.util.Set, class java.lang.InstantiationException thrown in constructor 

一个:662)

回答

0

貌似问题是与数据绑定。您必须先使用权限创建用户,然后使用UserRole域添加角色。喜欢的东西:

Role role = Role.findByAuthority("ROLE_USER") 
User user = new User(username:params.username,email:params.email,password:params.password,enabled:params.enabled,  accountExpired:params.accountExpired,accountLocked:params.accountLocked,passwordExpired:params.passwordExpired) 
new UserRole(user: user, role: role).save(flush: flush, insert: true) 
user.save(flush:true) 

欲了解更多信息如何创建使用Spring Security的用户,你可能想看看Spring Security UI

+0

使用数据库的原生支持时,这可能是正确的。然而,似乎通过摆弄您的detailService来实现MongoDB的连接并不真正关心这个问题的UserRole属性。然而,我现在能够保存我的物品,但我还是遇到了一些其他问题。 –