Grails 圣杯;spring security获取当前登录用户的id

Grails 圣杯;spring security获取当前登录用户的id,grails,spring-security,Grails,Spring Security,这是Spring安全服务中getCurrentUser()方法的示例代码 class SomeController { def springSecurityService def someAction() { def user = springSecurityService.currentUser } } 如何获取登录用户的id并在控制器中的此显示配置文件中使用它: springSecurityService.currentUser.id 在控制器中: spr

这是Spring安全服务中getCurrentUser()方法的示例代码

class SomeController {
    def springSecurityService

    def someAction() {
    def user = springSecurityService.currentUser
   }
}
如何获取登录用户的id并在控制器中的此
显示配置文件中使用它:

springSecurityService.currentUser.id
在控制器中:

springSecurityService.currentUser.id

一般来说,它在“主体”中可用,这是
GrailsUser
的一个实例,因此最有效的方法是
springSecurityService.principal.id

一般来说,它在“主体”中可用,这是
GrailsUser
的一个实例,因此,最有效的方法是
springSecurityService.principal.id

这似乎不起作用
Show Profile
,我的电脑中已经有了这个
def springSecurityService
controller@MuduM此代码必须在控制器中,而不是GSP中。服务当然不会被注入普惠制。在控制器中,您必须将生成的id添加到模型中,然后才能在GSP中使用它。这是一种非常低效的方法。
getCurrentUser()
方法使用存储在
身份验证中的内存中的信息进入数据库,然后获取实例的id。但是它已经存在于内存中,不会浪费数据库调用。查看我的答案。这似乎不起作用
Show Profile
,我的电脑中已经有了这个
def springSecurityService
controller@MuduM此代码必须在控制器中,而不是GSP中。服务当然不会被注入普惠制。在控制器中,您必须将生成的id添加到模型中,然后才能在GSP中使用它。这是一种非常低效的方法。
getCurrentUser()
方法使用存储在
身份验证中的内存中的信息进入数据库,然后获取实例的id。但是它已经存在于内存中,不会浪费数据库调用。看看我的答案。