【问题标题】:authenticatedUser is null but principle is not nullauthenticatedUser 为 null 但原则不为 null
【发布时间】:2014-10-07 06:16:09
【问题描述】:

Grails 安全新手在这里。我正在浏览文档并尝试了这个。 成功登录后,即使填充了主体,为什么经过身份验证的用户为空。我还有什么需要做的吗?

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)( 通常这样做的典型方式)。

【问题讨论】:

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


    【解决方案1】:

    需要在控制器中注入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很清楚,看看吧!

    【讨论】:

    • 我认为不需要注入 springSecurityService ,除非我直接在我的代码中使用它。 isLoggedIn() 是一种方便的访问器方法。但我的问题是,无论我注入与否,isLoggedIn 都有效。校长也有人居住。我遇到问题的是经过身份验证的用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多