[英]Grails Spring Security Plugin - ONE-TO-ONE association with User domain
我正在使用Spring Security Plugin來管理我的Grails應用程序中的成員資格和身份驗證。
我正在嘗試通過一對一關聯將User域類與Profile域相關聯。
我在User.groovy上添加了這些行:
static hasOne = [userProfile:UserProfile]
static constraints = {
//...
userProfile unique:true
}
和UserProfile.groovy:
User user
唉,我在調用UseRole.create(用戶,角色)時遇到錯誤。
關於如何獲得我正在尋找的相同功能,有一些最佳實踐。 特別是,我想將任何用戶與一個配置文件對象相關聯以擴展它。
我還想在帖子和其他表格中添加一對多的關系......
最誠摯的問候
PS:我得到了這個錯誤:
配置Spring Security UI ... 2011-03-08 12:18:51,179 [main] ERROR context.GrailsContextLoader - 在com.dromedian.xxxxx處執行bootstraps時出錯:$ Proxy19.save(未知來源)的null java.lang.NullPointerException .security.UserRole.create(UserRole.groovy:32)位於grails.util.Environment的BootStrap $ _closure1.doCall(BootStrap.groovy:20)的com.dromedian.xxxxx.security.UserRole $ create.call(未知來源) .evaluateEnvironmentSpecificBlock(Environment.java:251)at grails.util.Environment.executeForEnvironment(Environment.java:244)at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)at 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)_GrailsSettings_groovy $ _run_closure10.doCall(_GrailsSettings_groovy :280)在_GrailsSettings_groovy $ _ run_closure10.call(_GrailsSettings_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)at gant.Gant $ _dispatch_closure5.doCall(Gant.groovy:381)at gant.Gant $ _dispatch_closure7.doCall(Gant.groovy:415)at RunApp $ _run_closure1.doCall(RunApp.groovy:33) gant.Gant $ _dispatch_closure7.doCall(Gant.groovy)at gant.Gant.withBuildListeners(Gant.groovy:427)at gant.Gant.this $ 2 $ withBuildListeners(Gant.groovy)at gant.Gant $ this $ 2 $ withBuildListeners.callCurrent (來源)gant.Gant.dispatch(Gant.groovy:415)at gant.Gant.this $ 2 $ dispatch(Gant.groovy)at gant.Gant.invokeMethod(Gant.groovy)at gant.Gant.executeTargets(Gant .groovy:590)at gant.Gant.executeTargets(Gant.groovy:589)應用程序上下文關閉...
配置是:
User.groovy(Spring安全插件創建的域類)
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
沒有錯誤。 我試着調試,但我可以理解錯誤在哪里。
如前所述,由於需要用戶配置文件,因此不會保存測試用戶。 用戶的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
我剛剛遇到了類似的問題...並推斷出這個:
您的鏈接類必須明確允許為null ...即:
static constraints = {
...
userDetail unique:true, nullable: true
...
}
如果你不這樣做,你的類的構造函數調用失敗(正如其他人指出的那樣,嘗試在null上創建UserRole失敗)
我認為你必須在構造函數中設置UserProfile。 因為您沒有提供,所以save()失敗,從而為您提供NullPointerException
。
UserDetail profile = new UserDetail()
def testUser = new User(username: 'me', enabled: true, password: password, userdetail: profile)
assert testUser.save()
斷言.save()調用已被證明是非常有用的,因為當您更改域類中的某些代碼時,如果您忘記更改它們,那么構造函數將無法在引導程序文件中工作。 由於grails處理相當優雅,你得到奇怪的錯誤,而不是得到最有幫助的消息。 通過放置斷言,它將直接停在問題所在的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.