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

时间:2017-09-05 08:31:43

标签: grails spring-security

您好我正在尝试使用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。我错过了什么?

请指教。提前谢谢!

1 个答案:

答案 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  + "]"


    }

}