authenticatedUser为null但原则不为null

时间:2014-08-13 18:41:16

标签: grails spring-security grails-2.0 grails-plugin

新手Grails安全在这里。我正在阅读文档并尝试了这一点。 成功登录后,即使填充了principal,为什么经过身份验证的用户仍为null。还有什么我需要做的吗?

class SecureController {

 @Secured(['ROLE_ADMIN'])
 def userInfo(){
     if (isLoggedIn()){
      //authenticatedUser is null though user is authenticated.
      render authenticatedUser
      //render principal
    }
 }
}

来自文档

getAuthenticatedUser

  

从与当前对应的数据库中加载用户域类实例   已验证的用户,如果未经过身份验证,则为null。这是   相当于为springSecurityService添加依赖注入   并打电话   PersonDomainClassName.get(springSecurityService.principal.id)(   经常这样做的典型方式。)

1 个答案:

答案 0 :(得分:0)

您需要在控制器中注入springSecurityService

class SecureController {

 def springSecurityService

 @Secured(['ROLE_ADMIN'])
 def userInfo(){
     if (springSecurityService.isLoggedIn()){
      //authenticatedUser is null though user is authenticated.
      render authenticatedUser
      //render principal
    }
 }
}

您还可以使用springSecurityService.getCurrentUser()来检查您是否拥有有效的登录用户并进行渲染。 spring security core documentation非常清楚,请查看!