2010-01-20 103 views
18

我正在嘗試使用JDBC身份驗證來設置Spring 3安全性。除了當我嘗試指定多個訪問角色到一個攔截URL時,一切都工作正常。例如,我想任何人的角色ROLE_USER和ROLE_ADMIN到能夠訪問的所有頁面,我用的是follwoing線在我的Spring配置文件 -Spring Security 3指定了多個攔截URL訪問角色

<security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN" /> 

然而,這引發以下錯誤 -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [ ROLE_ADMIN] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:558) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:852) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:422) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:516) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:578) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) 
Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [ ROLE_ADMIN] 
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:154) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398) 
    ... 27 more 

如果指定只有其中一個角色可以訪問任何url,那麼它是好的(適用於任何角色)。改變我指定角色的順序也沒有區別。就好像Spring Security 3中的某些東西已經發生了變化,現在無法處理指定的多個訪問角色。

我以前使用Spring Security 2成功獲得了此工作,並且使用了幾乎相同的配置。有任何想法嗎?

我的安全配置文件列表如下 -

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:security="http://www.springframework.org/schema/security" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <security:http auto-config="true" access-denied-page="/denied.jsp" >  
     <security:form-login 
      default-target-url="/app/home" 
      always-use-default-target="true" /> 

     <security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN" /> 

     <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="" /> 
    </security:http> 

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:jdbc-user-service data-source-ref="dataSource" 
       users-by-username-query='select "username", "password", "enabled" 
        from users where "username" = ?' 
       authorities-by-username-query='select "username", "authority"      from user_roles where "username" = ?' /> 
     </security:authentication-provider> 
    </security:authentication-manager> 
</beans> 

回答

-2

我決定降級到春季安全2.0.5在不改變任何東西,以檢查這是否是一個錯誤在3,和LO-和觀看的時候是!

我想我也發現了相關的開放源漏洞在這裏 - https://jira.springsource.org/browse/SEC-1342

解決方案 - 使用2.0.5如果要使用此功能。

45

我有同樣的問題,但所用的詞句來解決這個問題:

你應該在你現有的配置嵌入

use-expressions="true" 

。所以:

<security:http auto-config="true" access-denied-page="/denied.jsp" > 

成爲

<security:http auto-config="true" access-denied-page="/denied.jsp" use-expressions="true"> 

然後:

<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" /> 
11

我不知道這個問題,其實我目前使用它在我的項目,並沒有看到問題。嘗試刪除後的空間,「我」的意思是嘗試使用ROLE_USER,ROLE_ADMIN

+3

Teja你是對的,它不能處理逗號後的空格。它工作正常,沒有空格。 – user223695 2010-01-21 09:00:32

+0

錯誤消息表明這確實是問題: 「不支持的配置屬性:[ROLE_ADMIN]」 – 2010-09-17 09:16:11

4

我有同樣的問題,並找到答案here
使用該行授予與這兩個角色訪問用戶:
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER') and hasRole('ROLE_ADMIN')" />
如果要授予訪問權限的用戶與列出的角色之一,用途:
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" />

此外,您還需要添加使用能力在您的安全* .xml SpEL中,將use-expressions="true"添加到<http>標記。

7

當試圖從Spring 3.x遷移到4.x時,我遇到了同樣的問題。最後,我發現默認情況下Spring 4.x中的「http」標記的參數「use-expressions」變爲「true」,而不是false(因爲它是舊版本)。

P.S.這個問題現在已經很老了,但是我在Google中發現了這個問題。所以其他人也可以找到它,然後這個信息可能會有用。

相關問題