0

我在 Grails 中使用 Spring Security 来限制对我的控制器的访问。我有一个用例,我想检查用户是否分配了多个角色。我意识到我可以制作另一个与“人有这两个角色”同义的角色,但这需要比我想要的更多的改变。

Spring Security 有一个 OR 版本表达式来检查用户是否具有任何角色列表:

//allow user to access if he has role ROLE_ADMIN -OR- ROLE_USER
//note this is shortcut notation for hasAnyRole(["ROLE_ADMIN","ROLE_USER"])
@Secured(["ROLE_ADMIN","ROLE_USER"])
def index = {}

有没有一种方法,或者只是使用Spring Expression Language (SpEL)来执行以下操作:

//allow user to access if he has role ROLE_ADMIN -AND- ROLE_USER
@Secured("hasAllRole(['ROLE_ADMIN','ROLE_USER']")
def index = {}

注意:SpringSecurityUtils 类确实有方法

public static boolean ifAllGranted(final String roles)
4

3 回答 3

3
hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')

再简单不过了

于 2011-11-01T17:08:26.500 回答
2

如果你使用@PreFilter而不是@Securedthen 你可以写:

@PreFilter("hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')")
于 2011-11-01T17:12:57.777 回答
1

你不能吗hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')

于 2011-11-01T17:08:09.507 回答