【问题标题】:grails Spring security custom permission in controller控制器中的grails Spring安全自定义权限
【发布时间】:2015-10-01 20:11:24
【问题描述】:

我想使用 grails spring 安全插件创建一个自定义权限处理程序。

假设我有一个 User 类和一个具有多对多关联的 company 类。

我只想让属于公司的用户调用一个名为“删除公司”的方法。示例:

class User {

static hasMany= [companies:Company]
static belongsTo = [Company]

}

class Company {

static hasMany = [users:User]

} 

控制器动作如下所示:

def deleteCompany(Long id) {

}

我只想允许用户调用属于公司的方法。所以当

assert Company.get(id}.users.find { it == currentUser }

这只是一个简化的例子。实际结构要复杂得多。这就是为什么我想为此使用 Spring Security 的力量。

我已经玩过 Spring Security ACL,但似乎我只能在服务中使用自定义权限,而不能在控制器中使用

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    您可以在控制器中使用beforeInterceptor

    def springSecurityService
    def beforeInterceptor=[action:this.&auth]
    private auth = {
        def toBeCheckedId=params.id
        if(toBeCheckedId 
              && Company.get(toBeCheckedId}.users.find { it == springSecurityService.currentUser }){
                redirect action:someHandlingAction
                return false
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2012-06-07
      • 2017-10-27
      • 2023-03-05
      • 2015-09-01
      相关资源
      最近更新 更多