【问题标题】:Grails Spring Security Plugin - ONE-TO-ONE association with User domainGrails Spring Security Plugin - 与用户域的一对一关联
【发布时间】:2011-03-02 15:34:44
【问题描述】:

我正在使用 Spring Security Plugin 在我的 Grails 应用程序中管理成员资格和身份验证。

我正在尝试通过一对一关联将 User 域类与 Profile 域相关联。

我在 User.groovy 上添加了这些行:

static hasOne = [userProfile:UserProfile]
static constraints = {
//...
             userProfile unique:true
}

到 UserProfile.groovy:

User user

唉,我在调用 UseRole.create(user,role) 时出错了。

关于如何获得我正在寻找的相同功能,有一些最佳实践。特别是,我想将任何用户与一个配置文件对象相关联以对其进行扩展。

然后我还想添加与帖子和其他表的一对多关系...

谢谢 最好的问候

PS: 我收到此错误:

配置 Spring Security UI ... 2011-03-08 12:18:51,179 [main] 错误 context.GrailsContextLoader - 执行引导程序时出错:null java.lang.NullPointerException 在 $Proxy19.save(未知来源) 在 com.dromedian.xxxxx.security.UserRole.create(UserRole.groovy:32) 在 com.dromedian.xxxxx.security.UserRole$create.call(未知来源) 在 BootStrap$_closure1.doCall(BootStrap.groovy:20) 在 grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251) 在 grails.util.Environment.executeForEnvironment(Environment.java:244) 在 grails.util.Environment.executeForCurrentEnvironment(Environment.java:220) 在 org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212) 在 grails.web.container.EmbeddableServer$start.call(未知来源) 在 _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158) 在 _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy) 在 _GrailsS​​ettings_groovy$_run_closure10.doCall(_GrailsS​​ettings_groovy:280) 在 _GrailsS​​ettings_groovy$_run_closure10.call(_GrailsS​​ettings_groovy) 在 _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149) 在 _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy) 在 _GrailsRun_groovy.runInline(_GrailsRun_groovy:116) 在 _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy) 在 _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59) 在 RunApp$_run_closure1.doCall(RunApp.groovy:33) 在 gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy) 在 gant.Gant.withBuildListeners(Gant.groovy:427) 在 gant.Gant.this$2$withBuildListeners(Gant.groovy) 在 gant.Gant$this$2$withBuildListeners.callCurrent(未知来源) 在 gant.Gant.dispatch(Gant.groovy:415) 在 gant.Gant.this$2$dispatch(Gant.groovy) 在 gant.Gant.invokeMethod(Gant.groovy) 在 gant.Gant.executeTargets(Gant.groovy:590) 在 gant.Gant.executeTargets(Gant.groovy:589) 应用程序上下文正在关闭...

配置是:

User.groovy(spring security插件创建的域类)

    static hasOne = [userDetail:UserDetail]

static constraints = {
    username blank: false, unique: true
    password blank: false
            userDetail unique:true
}

UserDetail.groovy

static hasOne = [user:User]
static belongsTo = User

BootStrap.groovy

    //TODO temporary added - no for production or persistent db
    def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    def userRole = new Role(authority: 'ROLE_USER').save(flush: true)

    String password = springSecurityService.encodePassword('password')
    def testUser = new User(username: 'me', enabled: true, password: password)
    testUser.save(flush: true)
    if(testUser != null){
        UserRole.create testUser, adminRole, true
    }

如果我不打电话

        UserRole.create testUser, adminRole, true

没有错误。我尝试调试,但我能理解错误在哪里。

【问题讨论】:

  • 您需要向我们显示错误消息,因为我们没有神奇的力量来查看您的屏幕。 ;)
  • 您必须更具体地了解您收到的错误消息的位置和内容。
  • 对不起,我现在发布错误信息:)

标签: security spring plugins grails grails-orm


【解决方案1】:

如前所述,您的测试用户未保存,因为需要用户配置文件。用户的 save 方法将返回 null,但是,您不检查它。 我通常将这些方法放在:

def ensureSave(domainObject) {
 if(!domainObject.save(flush:true)) {
  throw new Exception("not saved successfully: $domainObject");
 }
 domainObject
}

然后引用如下:

ensureSave(testUser)

testUser = ensureSave(new User(...));

HTH

【讨论】:

    【解决方案2】:

    我认为您必须在构造函数中设置 UserProfile。因为您没有提供,所以 save() 失败,从而给您NullPointerException

    UserDetail profile = new UserDetail()
    def testUser = new User(username: 'me', enabled: true, password: password, userdetail: profile)
    assert testUser.save()
    

    事实证明,对 .save() 调用的断言非常有用,因为当您更改域类中的某些代码时,如果您忘记更改它们,您的构造函数将无法在引导文件中工作。由于 grails 相当优雅地处理了这个问题,你会得到奇怪的错误,而不是在最能帮助你的地方得到消息。通过放置断言,它将直接在问题所在的地方停止。

    【讨论】:

      【解决方案3】:

      我刚刚遇到了一个类似的问题......并推断出这个:

      您的链接类必须明确允许为空...即:

      static constraints = {
         ...
         userDetail unique:true, nullable: true
         ...
      }
      

      如果您不这样做,则您的类的构造函数调用将失败(正如其他人指出的那样,尝试在 null 上创建 UserRole 失败)

      【讨论】:

        猜你喜欢
        • 2012-11-19
        • 2011-08-31
        • 2014-10-15
        • 2017-08-23
        • 2015-01-25
        • 1970-01-01
        • 2017-10-09
        • 2011-02-18
        • 2015-12-28
        相关资源
        最近更新 更多