Grails 检查特定控制器操作的角色访问权限

2024-01-07

我需要显示/隐藏操作按钮,具体取决于用户是否可以访问(通过角色定义)特定的控制器/操作。我正在使用 Spring Security 插件。

我的目标是对每个控制器的每个方法使用注释 @Secured("ROLE_...") 并且我正在寻找一种方法来在调用操作之前检查用户是否有权访问此特定操作。我猜有办法检查这一点,因为注释带来了信息,但我找不到任何解决方案。

在此示例中,我尝试找到 HASACCESS 方法:

带有@Secured注解的控制器

    class MyExampleController{

      @Secured("ROLE_ADMIN")
      def myMethod(){ do stuff.. }

    }

包含去链接的 HTML 代码

    <role:link controller="myExample" action="myMethod">Action</role:link>

和我的 tagLib 角色

    class RoleTagLib {
     static namespace = "role"

      def link = {attrs, body ->
        User user = (User) springSecurityService.currentUser          
        if(HASACCESS(user, attrs.controller, attrs.action)){
          out << g.link(attrs, body)
        }
      }
    }

我发现在这个线程 https://stackoverflow.com/questions/2306492/grails-with-springsecurity-check-if-the-current-user-can-access-controller-acSecurityTagLib 中包含的“hasAccess()”方法,但此方法受到保护,即使当我用我的扩展 SecurityTagLib 时,此方法的调用也会返回“没有方法签名...”。我认为它使用 Config.groovy 中定义的拦截UrlMap,而不是注释。

编辑:我成功扩展了安全 tagLib 并使用“hasAccess”方法,但似乎它仅使用 Config.groovy 中包含的拦截UrlMap,并且不关心我在控制器中放入的注释。


Use

<sec:ifAllGranted roles="ROLE_ADMIN,ROLE_SUPERVISOR">
  secure stuff here
</sec:ifAllGranted>

or

<sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SUPERVISOR">
    secure stuff here
</sec:ifAnyGranted>

根据Spring Security Core Grails 插件文档 http://grails-plugins.github.io/grails-spring-security-core/guide/helperClasses.html#securityTagLib.

或者只是将 Spring security 核心 taglib 与您的标签库一起使用。

class RoleTagLib {
  static namespace = "role"

  SpringSecurityService springSecurityService

  def link = { attrs, body ->
    User user = (User) springSecurityService.currentUser          
    sec.ifAnyGranted(roles: 'ROLE_ADMIN,ROLE_SUPERVISOR'){
      out << g.link(attrs, body)
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Grails 检查特定控制器操作的角色访问权限 的相关文章

随机推荐