具有多个角色的Spring安全访问

时间:2014-06-03 08:36:58

标签: java spring spring-security

我想为具有以下角色之一的用户(ROLE1或ROLE2)定义某些页面的访问权限

我正在尝试在spring security xml文件中对此进行配置,如下所示:

<security:http entry-point-ref="restAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security" use-expressions="true">
        <!-- skipped configuration -->
        <security:intercept-url pattern="/rest/api/myUrl*" access="hasRole('ROLE1') or hasRole('ROLE2')" />

        <!-- skipped configuration -->
    </security:http>

我尝试过各种方式:

access="hasRole('ROLE1, ROLE2')"
access="hasRole('ROLE1', 'ROLE2')"
access="hasAnyRole('[ROLE1', 'ROLE2]')"

但似乎没有任何效果。

我一直在异常

java.lang.IllegalArgumentException: Unsupported configuration attributes:

java.lang.IllegalArgumentException: Failed to parse expression 'hasAnyRole(['ROLE1', 'ROLE2'])'

应如何配置?

由于

3 个答案:

答案 0 :(得分:26)

如何尝试将,分开。请参阅文档herehere

<security:intercept-url pattern="/rest/api/myUrl*" access="ROLE1,ROLE2"/>

OR

hasAnyRole('ROLE1','ROLE2')

答案 1 :(得分:1)

问题是我配置了自定义access-decision-manager-ref="accessDecisionManager" 并没有通过其中一个选民。 通过将org.springframework.security.web.access.expression.WebExpressionVoter添加到accessDecisionManager bean来解决。

答案 2 :(得分:-3)

如果您正在使用OAuth2播放,请务必检查此问题以解决问题。

http://lightweightyes.blogspot.in/2012/08/spring-security-expressions-not-working.html

我只是花了很多时间来尝试所有的试验和错误。

相关问题