gpt4 book ai didi

grails - Grails Controller 中的Spring Security自定义权限

转载 作者:行者123 更新时间:2023-12-02 16:00:37 24 4
gpt4 key购买 nike

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

想象一下,我有一个用户类和一个具有多对多关联的公司类。

我只允许用户在属于公司时调用称为“删除公司”的方法。例:

class User {

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

}

class Company {

static hasMany = [users:User]

}

Controller Action 如下所示:
def deleteCompany(Long id) {

}

我只想允许用户调用属于公司的方法。所以什么时候
assert Company.get(id}.users.find { it == currentUser }

这只是一个简化的示例。实际的结构要复杂得多。这就是为什么我要为此使用spring security的功能。

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

最佳答案

您可以在 Controller 中使用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
}
}
}

关于grails - Grails Controller 中的Spring Security自定义权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com