我在Grails中使用Spring Security来限制对我的控制器的访问。我有一个用例,在这个用例中我想检查一个用户是否分配了多个角色。我意识到我可以只做另一个角色,这个角色与“个人拥有这两个角色”是同义词,但它需要比我想要的更多的改变。
Spring Security有一个OR版本的expression来检查用户是否有角色列表中的任何一个:
//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)发布于 2011-11-02 01:12:58
如果你使用@PreFilter而不是@Secured,那么你可以这样写:
@PreFilter("hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')")发布于 2011-11-02 01:08:27
hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')再简单不过了
发布于 2011-11-02 01:08:10
你能不能不做hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
https://stackoverflow.com/questions/7970029
复制相似问题