gpt4 book ai didi

grails - 带有Grails的Spring Security Service-获取当前用户 'NULL'错误

转载 作者:行者123 更新时间:2023-12-02 15:46:55 26 4
gpt4 key购买 nike

嗨,我正在尝试使用Grails的Spring Security Core,并尝试获取当前登录的用户。但是,我得到了java.lang.NullPointerException,它是由以下原因引起的:无法在空对象上获取属性“principal”。

以下是我的代码:

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import grails.plugin.springsecurity.annotation.Secured

@Secured(['ROLE_ADMIN'])
@Transactional
class MyController {

static allowedMethods = [save: "POST", update: "PUT", delete:
"DELETE"]

def test(){
def springSecurityService
render "Current User is: [" + springSecurityService.principal
+ "]"
}
}

由于某些原因,springSecurityService为null。我错过了什么?

请指教。先感谢您!

最佳答案

正如约书亚·摩尔(Joshua Moore)所建议的那样,此方法有效!非常感谢!

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import grails.plugin.springsecurity.annotation.Secured

@Secured(['ROLE_ADMIN'])
@Transactional
class MyController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def springSecurityService

def test(){



render "Current User is: [" + springSecurityService.principal + "]"


}

}

关于grails - 带有Grails的Spring Security Service-获取当前用户 'NULL'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com