当我尝试从 Controller 获取当前登录用户ID时发生错误消息。

错误信息:
信息
没有方法签名:static grails.plugin.springsecurity.SpringSecurityService.getCurrentUserId()适用于参数类型:()值:[]可能的解决方案:getCurrentUserId(),getCurrentUser()

package com.testapp


import grails.plugin.springsecurity.annotation.Secured
import grails.plugin.springsecurity.SpringSecurityService

@Secured('IS_AUTHENTICATED_FULLY')
class TestController {

def myAction(){
   def user = SpringSecurityService.getCurrentUserId()
}

最佳答案

该方法不是静态的,因此您需要为Singleton Spring bean添加依赖项注入(inject)并将其作为实例方法调用:

package com.testapp

import grails.plugin.springsecurity.annotation.Secured
import grails.plugin.springsecurity.SpringSecurityService

@Secured('IS_AUTHENTICATED_FULLY')
class TestController {

   SpringSecurityService springSecurityService

   def myAction() {
      long userId = springSecurityService.getCurrentUserId()
   }
}

07-24 09:34