【问题标题】:Securing Controller Actions by User in Grails在 Grails 中保护用户的控制器操作
【发布时间】:2012-04-05 23:13:01
【问题描述】:

我在一个项目中使用 Grails 安全插件。我正在使用控制器操作上的注释来限制对某些类别的用户的访问,例如“ROLE_ADMIN”或“ROLE_USER”。

(以此作为我正在做的事情的基础:http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/5%20Configuring%20Request%20Mappings%20to%20Secure%20URLs.html#5.1%20Defining%20Secured%20Annotations

我的问题是,如何限制操作,以便用户只能看到有关他们自己的信息。例如,假设我有一个 id = 1 的用户。如果我有一个显示用户信息的操作:

mySite/User/Show/1

如何防止 id=1 的同一用户访问

mySite/User/Show/2

?有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: grails spring-security grails-plugin


    【解决方案1】:

    如果您想将相同的逻辑应用于多个操作,也可以使用 Grails 控制器拦截器

    class SomeController {
    
     def beforeInterceptor = [action: this.&checkUser ] 
    
       def springSecurityService
    
    def checkUser() {
        User user = User.get(params)
           User logged = User.get(springSecurityService.principal.id)
           if (user.id != logged.id) {
       {
           redirect(action: "accessDenied", controller='access' id: params.long("id")) //re-direct accessDenied page
       return false
       }
        return true;
    }
    

    }

       Class AccessController{
         def accessDenied= {
    
            render(view: "accessDenied")
    
        }
     }
    

    【讨论】:

      【解决方案2】:

      以下会有什么问题?:

      class SomeController {
          springSecurityService
          // other stuf ...
          def show () {
             User user = User.get(params)
             User logged = User.get(springSecurityService.principal.id)
             if (user.id != logged.id) {
                flash.message = "You can't see the profile of other users"
                redirect action:"list" // You can redirect to other controller/action
                return //Since grails 2 this is needed
             }
             // Logic for display your user
          }
          // other stuf ...
      }
      

      【讨论】:

      • 没问题,我只是想,既然这是一种常见的情况,就会有一种“正确”的方式来做到这一点。
      【解决方案3】:

      您所要求的是您的业务规则的一部分。所以你应该在你的代码中处理这些场景,而不是寻找一些插件或帮助代码。

      您可以做的是,确保访问用户详细信息的用户的 id 与被询问详细信息的用户的 id 相同。

      您也可以在对象级别进行此检查,但这意味着需要对数据库进行额外查询以获取用户详细信息。

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        我必须同意您正在尝试实施具有安全方面的业务规则。如果用户创建了某种文档,您不会使用授权来选择他们的个人资料页面上可见的内容,对吗?

        您必须在授权方面达到的位置和业务规则的开始位置划清界限。

        根据我的经验,为避免界限模糊,我总是使用授权角色作为与一组功能相关联的用户类型。特定的用户类型可以访问一系列故事或用例。这些用例仅限于特定角色。

        如果您开始询问有关数据可见性的问题(页面上隐藏的内容,取决于任何业务因素),那么您应该远离您的安全框架

        【讨论】:

        【解决方案5】:

        我不同意您需要重新定义业务逻辑与安全逻辑。这是一个常见的用例,授权应该涵盖它。这就是 Grails 有过滤器的原因。使用授权过滤器添加如下功能:

        class AuthorizationFilters {
            def filters = {
                userCheck(controller: 'user', action: '*') {
                    before = {
                        // Check current user id is param.id here
                    }
                }
            }
        }
        

        因此,您的安全逻辑在您的控制器之外。您可以添加其他控制器,如果它们传入用户 ID,甚至可以在此处检查域类是否由当前用户拥有的其他方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-30
          • 1970-01-01
          • 2012-01-19
          • 1970-01-01
          • 2011-08-16
          • 2013-11-13
          • 2014-02-13
          • 1970-01-01
          相关资源
          最近更新 更多