Spring Security的intercept-url访问权限=" hasRole()"属性给出错误

时间:2017-04-12 12:51:29

标签: spring spring-security

我正在尝试使用Spring security intercept-url来获取我的api请求以获得Oauth2安全性。但是每当我尝试使用

时,我都会收到错误 Unsupported configuration attributes
  

存取=" hasRole(' ROLE_ADMIN')"

请在下面找到我的版本,配置和错误详情:

版本:

1春季版 - 4.3.7.RELEASE

2 Spring Security版本 - 4.2.2.RELEASE

3 JRE系统库 - Java SE 1.8

B Spring XML配置:

为了保护我的资源,我编写了以下配置:

<!-- Protected resources -->
<http pattern="/api/**"
      create-session="never" 
      use-expressions="true" 
      entry-point-ref="oauthAuthenticationEntryPoint"
      access-decision-manager-ref="accessDecisionManager"
      xmlns="http://www.springframework.org/schema/security">
    <anonymous enabled="false"/>

    <!-- Intercept your api requests to get the Oauth2 Security -->
    <intercept-url pattern="/api/addEmployee**" access="hasRole('ROLE_ADMIN')"/>
    <intercept-url pattern="/api/getAllEmployees**" access="hasRole('ROLE_ADMIN')"/>

    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/>
    <custom-filter ref="corsHandler" after="PRE_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler"/>
</http>

<!-- Authentication in config file -->
<authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
    <authentication-provider user-service-ref="clientDetailsUserService"/>
</authentication-manager>

<authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
    <authentication-provider>
        <user-service id="userDetailsService">
            <user name="admin" password="password" authorities="ROLE_ADMIN"/>
        </user-service>
    </authentication-provider>
</authentication-manager>

<!-- Access Decision Manager Bean -->
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
  xmlns="http://www.springframework.org/schema/beans">
    <constructor-arg>
        <list>
            <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
            <bean class="org.springframework.security.access.vote.RoleVoter"/>
            <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
        </list>
    </constructor-arg>
</bean>

C服务器错误:

当我在Tomcat服务器上构建并运行我的项目时,出现以下错误:

  

引起:java.lang.IllegalArgumentException:不支持的配置属性:[hasRole(&#39; ROLE_ADMIN&#39;),hasRole(&#39; ROLE_ADMIN&#39;)]       在org.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:176)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)       ......还有47个

不知道为什么 hasRole()无效。

另外,请在下面找到Spring Security Reference html文档的链接:

Spring Security Reference 4.2.2 RELEASE

帮助将不胜感激..

0 个答案:

没有答案