Grails 使用Groovy Spring security getCurrentUser获取有意义的字符串
我为当前用户收集字符串的代码如下所示Grails 使用Groovy Spring security getCurrentUser获取有意义的字符串,grails,spring-security,Grails,Spring Security,我为当前用户收集字符串的代码如下所示 package timetracker2 import grails.timesecurity.* import timetracker2.TimeController class Time { def springSecurityService int id Project projectname Date dateCreated Date dateWorked Double timeSpent TimeC
package timetracker2
import grails.timesecurity.*
import timetracker2.TimeController
class Time {
def springSecurityService
int id
Project projectname
Date dateCreated
Date dateWorked
Double timeSpent
TimeClassification classification
String currentUser = {
String currentUser = springSecurityService.getCurrentUser()
def user = springSecurityService.getCurrentUser()
String username = user
return username
}
static constraints = {
timeSpent blank: false
classification blank: true
}
static mapping = {
table 'Time'
cache true
id column: 'Time_id'
dateCreated column: 'Date_created'
dateWorked column: 'Date_Worked'
timeSpent column: 'Time_Spent'
currentUser column: 'Username'
}
}
这是我拿出来的绳子。。。。
时间追踪器2.时间$_closure1@108115
这是怎么回事????任何帮助都将不胜感激
我也尝试过使用get authorization,并尝试在person类中添加一个toString方法。唉,没有成功 您正在初始化
currentUser
变量,其结果是对一个闭包调用toString()
,该闭包在调用时返回当前用户
你想要的是
String currentUser = springSecurityService.getCurrentUser()
它使用
springSecurityService.getCurrentUser()
返回的值初始化currentUser
变量。我怀疑这里的部分问题是您试图在设置springSecurityService
之前使用它。声明中的初始值设定项表达式
String currentUser = springSecurityService.getCurrentUser()
在构造对象时运行,但此时springSecurityService
引用为null,因为它仅在构造函数完成后被注入。您可以将引用springSecurityService
的代码放在beforeinter
事件中,这将延迟调用,直到对象第一次保存到数据库中。您可能需要principal.username
而不是getCurrentUser()
(感谢Burt Beckwith提供的指针)
以前尝试过,它总是导致如下错误
无法实例化test objecttimetracker2.Time
编辑您的问题。显示时间类的代码、使用时间类并导致此异常的代码,以及excat和完整的异常堆栈跟踪。根据请求,感谢您的帮助| Error 2013-02-02 14:20:44396[localhost-startStop-1]ERROR context.grailContextLoader-执行引导时出错:创建名为“transactionManagerPostProcessor”的bean时出错:bean初始化失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“transactionManager”的bean时出错:设置bean属性“sessionFactory”时无法解析对bean“sessionFactory”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常为org.hibernate.InstantiationException:无法实例化测试对象TimeTracker2。时间消息:创建名为“transactionManagerPostProcessor”的bean时出错:bean初始化失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“transactionManager”的bean时出错:设置bean属性“sessionFactory”时无法解析对bean“sessionFactory”的引用;嵌套异常isorg.springframework.beans.factory.BeanCreationException:创建名为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常为org.hibernate.InstantiationException:无法实例化test objecttimetracker2.TimeI已还原编辑,因为代码在右后没有任何意义。如果用户在编辑时可以更改,则必须在插入前和更新前进行编辑。但是另外getCurrentUser()
返回整个域类实例;应该是springSecurityService.principal.username
我猜我在这里做错了什么,因为我现在得到了以下错误<代码>无法在“Username”列、表“TimeTracker.dbo.Time”中插入NULL值;列不允许空值。插入失败。
非常接近,从未使用beforeInsert beforeInsert={String currentUser=springSecurityService.getCurrentUser()}def beforeUpdate={String currentUser=springSecurityService.getCurrentUser()}1,不要使用getCurrentUser()
-它不是字符串。使用springSecurityService.principal.username
#2、使用方法而不是闭包,即def beforeInsert(){…}
和def BEFOREINSUPDATE(){…}
相同的问题不能将值NULL插入列'Username',表'TimeTracker.dbo.Time';列不允许空值。INSERT失败def beforeInsert(){String currentUser=springSecurityService.principal.username}def beforeUpdate(){String currentUser=springSecurityService.principal.username}
class Time {
def springSecurityService
String currentUser
// other fields...
def beforeInsert() {
currentUser = springSecurityService.principal.username
}