我想使用Spring Security JSP taglibs基于角色有条件地显示一些内容。 但在Spring Security 3.1.x中只检查一个角色。如何使用<sec:authorize access =“hasRole('ROLES)”>检查多个角色?
我可以使用但是ifllllgranted已被弃用。
任何帮助?
我想使用Spring Security JSP taglibs基于角色有条件地显示一些内容。 但在Spring Security 3.1.x中只检查一个角色。如何使用<sec:authorize access =“hasRole('ROLES)”>检查多个角色?
我可以使用但是ifllllgranted已被弃用。
任何帮助?
有一个Spring Security的特殊安全表达式:
hasAnyRole(角色列表) - 如果用户已被授予任何 指定的角色(给出一个逗号分隔的列表的字符串)。
我从来没有使用它,但我认为这正是你在找什么。
实例:
<security:authorize access="hasAnyRole('ADMIN', 'DEVELOPER')">
...
</security:authorize>
这里是一个link to the reference documentation其中标准弹簧安全表达式描述。另外,这里是一个discussion,我描述了如何在需要时创建自定义表达式。
JSP页面上使用hasAnyRole可能会导致异常用于提供与单引号的角色时的方法类似下面,主要是当页面与jQuery合并处理的JSP页面:
<security:authorize access="hasAnyRole('ROLE_USER')"> ...
</security:authorize>
虽然这是赞成使用方法的弃用解决方案,我认为解决方案提供的here也是有用的。使用替代访问方法是ifAllGranted方法:
<security:authorize ifAllGranted="ROLE_USER"> ...
</security:authorize>
此外,请注意,包括JSP页面上的以下标签:
<%@taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
运行客户端的jQuery会如何影响jsp渲染,这会发生在服务器端? – 2015-07-22 17:32:34
我用hasAnyRole('ROLE_ADMIN','ROLE_USER')
,但我是越来越以下错误bean创建
Error creating bean with name 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource] while setting bean property 'securityMetadataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Expected a single expression attribute for [/user/*]
然后我试了
access="hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')"
它的工作对我来说很好。
作为我的用户之一是管理员以及用户。
为此,你需要添加use-expressions="true" auto-config="true"
其次是HTTP标签
<http use-expressions="true" auto-config="true" >.....</http>
@迪马斯的答案是不是你的问题逻辑一致的; ifAllGranted
不能直接替换为hasAnyRole
。
从Spring Security 3—>4 migration guide:
老:
<sec:authorize ifAllGranted="ROLE_ADMIN,ROLE_USER">
<p>Must have ROLE_ADMIN and ROLE_USER</p>
</sec:authorize>
新(SPEL):
<sec:authorize access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')">
<p>Must have ROLE_ADMIN and ROLE_USER</p>
</sec:authorize>
与hasAnyRole
更换ifAllGranted
将直接导致春季用OR
而不是评估的声明一个AND
。也就是说,hasAnyRole
将返回true
如果认证主要包含至少一个指定的角色,而Spring的(现在已经过时,因为Spring Security 4)ifAllGranted
方法得到确认的主要包含指定角色的所有才会返回true
。
TL; DR:要复制的ifAllGranted
使用Spring Security的标签库的新的认证表达式语言行为中,hasRole('ROLE_1') and hasRole('ROLE_2')
模式需要使用。
非常感谢。它同样适用于特权。
它的工作:-) <秒:授权访问= “hasAnyRole( 'ROLE_USER', 'ROLE_ADMIN')”> \t \t \t(用户/管理员) \t秒:授权>可是任何方式来检查hasAllRoles(role1,role2)? – 2012-07-13 11:40:52
关于你的最后一个问题:“但是检查hasAllRoles(role1,role2)的任何方法?” - 我不认为这是可能的。但你可以克里特你的自定义表达式(我给了一个链接),并在代码检查任何你想要的。 – dimas 2012-07-13 11:51:31
关于你的第二个问题,你应该可以使用hasRole('ADMIN')和hasRole('DEVELOPER')这样的'and'运算符来实现这个功能。 – bh5k 2013-04-01 21:20:28