我在 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)