Spring Security中的多个角色定义

时间:2018-04-12 12:49:43

标签: spring-security roles

我在Spring Security 4.2.5中有以下角色定义:

<security:http>
    <security:intercept-url pattern="/api/doSomething*"
                            access="ROLE_SOMETHING_COMPLETELY_DIFFERENT,ROLE_ONE,ROLE_TWO"/>
    <security:form-login />
    <security:logout />
</security:http>
由于以下异常,

和上下文无法加载:

Caused by: java.lang.IllegalArgumentException: Failed to parse expression "ROLE_SOMETHING_COMPLETELY_DIFFERENT,ROLE_ONE,ROLE_TWO"
at org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource.processMap(ExpressionBasedFilterInvocationSecurityMetadataSource.java:84)
at org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource.<init>(ExpressionBasedFilterInvocationSecurityMetadataSource.java:53)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 79 more

我正在从Spring Security 3.2进行升级,其中前面提到的代码段正常工作。以下不起作用:

  • access="ROLE_SOMETHING_COMPLETELY_DIFFERENT,ROLE_ONE,ROLE_TWO"
  • access="ROLE_SOMETHING_COMPLETELY_DIFFERENT, ROLE_ONE, ROLE_TWO"
  • access='ROLE_SOMETHING_COMPLETELY_DIFFERENT,ROLE_ONE,ROLE_TWO'
  • access='ROLE_SOMETHING_COMPLETELY_DIFFERENT, ROLE_ONE, ROLE_TWO'

然而,这有效:

access="hasAnyRole('ROLE_SOMETHING_COMPLETELY_DIFFERENT','ROLE_ONE','ROLE_TWO')

至少从我已经阅读过的内容中,没有任何迹象表明在Spring Security文档中access标记中的此类参数是不可解析的。相反,有很多examples using the exact same syntax

我希望在访问字段中保留相同的语法,因为项目的访问管理配置中有批次

编辑:使用4.3.15 Spring Core版本。

1 个答案:

答案 0 :(得分:2)

Spring Security 4.x更改了http元素use-expressions属性的默认值。见Migrating from Spring Security 3.x to 4.x (XML Configuration)

  

<强> 6.2。迁移

     

http @ use-expressions属性的默认值从false更改为true。这意味着如果未显式配置use-expression属性,则需要更新配置。例如,如果使用Spring Security 3.2.x的应用程序包含类似于以下内容的配置:

     

Spring Security 3.2.x示例配置

     
<http> 
  <intercept-url pattern="/login" access="ROLE_ANONYMOUS"/>
  <intercept-url pattern="/**" access="ROLE_USER"/>
  ...
</http>
     

观察未提供use-expressions属性。如果提供,则无需任何操作。   当Spring Security 4.x:

时,需要将配置更新为类似于以下内容的内容      

迁移到Spring Security 4配置

     
<http use-expressions="false"> 
  <intercept-url pattern="/login" access="ROLE_ANONYMOUS"/>
  <intercept-url pattern="/**" access="ROLE_USER"/>
  ...
</http>
     

我们明确提供了use-expressions属性。同样,如果已经提供了属性,则无需进行任何操作。