当我试图从控制器获取当前登录用户ID时发生错误消息。
错误讯息: 信息 没有方法签名:static grails.plugin.springsecurity.SpringSecurityService.getCurrentUserId()适用于参数类型:()values:[]可能的解决方案: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()
}
答案 0 :(得分:1)
该方法不是静态的,因此您需要为单例Spring bean添加依赖注入,并将其作为实例方法调用:
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()
}
}