我在从我的一个控制器创建新用户时遇到一些问题。我正在尝试向我的MongoDB用户集合添加一个新用户,如下所示。权限定义为域中的一组角色。
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 }
}
完全相同的代码能够从bootstrap成功地创建用户,但是当我试图从一个简单的控制器执行相同的操作时,我得到了这个错误:
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
a:662)
发布于 2012-09-24 18:04:56
看起来问题出在数据绑定上。您必须先创建具有权限的用户,然后使用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安全性的用户的更多信息,您可能需要查看Spring Security UI
https://stackoverflow.com/questions/12561728
复制相似问题