【问题标题】:Add association to SpringSecurity User class将关联添加到 SpringSecurity User 类
【发布时间】:2012-10-11 13:22:31
【问题描述】:

我正在尝试将默认 Spring Security 用户域类的引用添加到另一个应该包含其他用户信息的类。

class User {
    transient springSecurityService

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    Profile profile
      ...

...和...

class Profile {
    static constraints = {
    }

    static belongsTo = [user : User]

    String firstName
    String lastName     
}

从我读到的应该是它的工作方式,但我收到以下错误:

ERROR context.GrailsContextLoader  - Error executing bootstraps: null
Message: null
   Line | Method
->>  32 | create                           in com.app.UserRole
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    15 | doCall                           in BootStrap$_closure1
|   301 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|   294 | executeForEnvironment            in     ''
|   270 | executeForCurrentEnvironment . . in     ''
|   303 | innerRun                         in java.util.concurrent.FutureTask$Sync
|   138 | run . . . . . . . . . . . . . .  in java.util.concurrent.FutureTask
|   886 | runTask                          in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . . . . . . . . . . . . .  in     ''
^   662 | run                              in java.lang.Thread

有什么想法吗?

【问题讨论】:

  • 请向我们展示您从 BoorStrap.groovy 生成代码时的错误...

标签: grails spring-security grails-orm


【解决方案1】:

我们需要看看你的BootStrap 才能确定,但​​我怀疑这是因为你正在做类似的事情

def user = new User(....).save()
def role = new Role(....).save()
UserRole.create(user, role)

而您的新 User 验证失败(导致 save 返回 null)。

如果你使用save(failOnError:true),你应该得到一个不同的异常,更好地表明真正的问题是什么。检查您的User 类中是否有正确的constraints,特别注意GORM 属性默认情况下不可为空,因此如果您希望能够保存没有ProfileUser您将需要添加profile(nullable:true) 的约束。

【讨论】:

  • 设置 nullable: true 约束做到了。非常感谢!
猜你喜欢
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 2013-03-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
相关资源
最近更新 更多