【问题标题】:Grails and Spring Security: How do I get the authenticated user from within a controller?Grails 和 Spring Security:如何从控制器中获取经过身份验证的用户?
【发布时间】:2010-10-20 23:26:18
【问题描述】:

我最近从 JSecurity 插件转移到了 Spring Security。如何从我的控制器中获取经过身份验证的用户?

【问题讨论】:

    标签: grails


    【解决方案1】:

    使用此代码:

    if (springSecurityService.isLoggedIn()){   
            println "Logged In"
    
        }
    

    【讨论】:

      【解决方案2】:

      你也可以通过这种方式获取当前用户

       class AnyController {
        def springSecurityService
        def someAction = { 
          def user = User.get(springSecurityService.principal.id)
      
           } 
       }
      

      【讨论】:

        【解决方案3】:

        现在,我认为这样做的方法是:

        def user = getAuthenticatedUser()
        

        【讨论】:

          【解决方案4】:

          以下代码来自Spring Security Core Plugin (Version: 1.1.2) - Reference Documentation - Section 6.2

          grails.plugins.springsecurity.SpringSecurityService 提供安全实用功能。它是一个常规的 Grails 服务,因此您使用依赖注入将其注入到控制器、服务、taglib 等中:

          class SomeController {
              def springSecurityService
              def someAction = { 
                  def user = springSecurityService.currentUser 
                  …
              } 
          }
          

          【讨论】:

            【解决方案5】:

            目前没有文档记录,但在插件安装文件中,它向每个控制器添加了 3 种方法,因此您实际上不必注入 authenticationService:

            private void addControllerMethods(MetaClass mc) {
                mc.getAuthUserDomain = {
                    def principal = SCH.context?.authentication?.principal
                    if (principal != null && principal != 'anonymousUser') {
                        return principal?.domainClass
                    }
            
                    return null
                }
            
                mc.getPrincipalInfo = {
                    return SCH.context?.authentication?.principal
                }
            
                mc.isUserLogon = {
                    def principal = SCH.context?.authentication?.principal
                    return principal != null && principal != 'anonymousUser'
                }
            }
            

            这意味着你可以调用

            principalInfo
            

            获取主体对象。它还具有“isUserLogin”以查看用户是否已登录,并具有“authUserDomain”以获取与已登录用户的主体相关联的实际域类实例(Person/User)。

            【讨论】:

            【解决方案6】:

            我使用的是 0.5.1,以下对我有用:

            class EventController {
              def authenticateService
            
              def list = { 
                 def user = authenticateService.principal() 
                 def username = user?.getUsername()
                 .....
                 .....
              } 
            }
            

            【讨论】:

            • 太好了,谢谢!只是上面代码中的一个错字。它应该是 def username = user?.getUsername()
            • 请注意新版本的 Spring Security 有一些变化。 See here for example code for Spring Security version 1.1.2
            • 还要注意,如果你使用服务,那么初始化不应该在方法体中,而是在类中。
            猜你喜欢
            • 2014-01-12
            • 2013-04-23
            • 2014-10-03
            • 2015-10-28
            • 1970-01-01
            • 2020-10-20
            • 1970-01-01
            • 2013-03-31
            • 2011-03-22
            相关资源
            最近更新 更多