您好我正在尝试使用Grails的Spring Security Core并尝试获取当前登录的用户。但是,我得到java.lang.NullPointerException,这是由以下原因导致的:无法获取属性' principal'在null对象上。
以下是我的代码:
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。我错过了什么?
请指教。提前谢谢!
答案 0 :(得分:0)
正如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 + "]"
}
}