【问题标题】:grails spring-security create usergrails spring-security 创建用户
【发布时间】:2012-09-15 17:13:41
【问题描述】:

我从我的一个控制器创建新用户时遇到了一些问题。我正在尝试像这样将新用户添加到我的 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 }
      }

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

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)

【问题讨论】:

    标签: mongodb grails groovy spring-security


    【解决方案1】:

    看起来问题出在数据绑定上。您必须先创建具有权限的用户,然后使用 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

    【讨论】:

    • 这在使用数据库的本机支持时可能是正确的。但是,通过摆弄您的 detailService 来实现与 MongoDB 的连接似乎并不真正关心该问题的 UserRole 属性。但是我现在可以保存我的物品,但我遇到了其他一些问题。
    猜你喜欢
    • 2012-06-08
    • 2012-07-26
    • 2015-06-02
    • 2015-03-27
    • 2011-10-31
    • 2012-09-20
    • 2014-03-17
    • 2013-06-18
    • 1970-01-01
    相关资源
    最近更新 更多