Spring Security 是否有“hasAllRole”?执行“hasAnyRole”的 AND 版本
我在 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 表达式语言 (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)
I'm using Spring Security in Grails to restrict access to my controllers. I have a use case where I want to check that a user has multiple roles assigned. I realize I could just make another role that is synonymous with 'person has these two roles', but it would require much more changes than I'd like.
Spring Security has an OR version expression to check if a user has any of a list of roles:
//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 = {}
Is there a method, or just way using Spring Expression Language (SpEL) to do the following:
//allow user to access if he has role ROLE_ADMIN -AND- ROLE_USER
@Secured("hasAllRole(['ROLE_ADMIN','ROLE_USER']")
def index = {}
Note: SpringSecurityUtils class does have the method
public static boolean ifAllGranted(final String roles)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
再简单不过了
Couldn't be much simpler
如果您使用
@PreFilter
而不是@Secured
那么您可以编写:If you use
@PreFilter
instead of@Secured
then you can write:你不能做
hasRole('ROLE_ADMIN')和hasRole('ROLE_USER')
吗?could you not do
hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')
?