在使用键[0]设置bean属性'sourceList'时,无法解析对bean'org.springframework.security.web.DefaultSecurityFilterChain#0'的引用

时间:2014-02-07 09:47:25

标签: java spring spring-security

我必须在我的应用程序中添加spring security 3.2,这是我的配置文件:

Spring-security.xml

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

  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<!-- This is where we configure Spring-Security  -->

<security:http  auto-config="true" use-expressions="true" access-denied-     page="/auth/denied" >
<security:intercept-url pattern="/auth/login" access="permitAll"/>
<security:intercept-url pattern="/main/admin" access="hasRole('ROLE_ADMIN')"/>
<security:intercept-url pattern="/**" access="ROLE_USER" />

<security:form-login
login-page="/auth/login"
authentication-failure-url="/auth/login?error=true"
default-target-url="/main/common"/>

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

</security:http>

<!-- Declare an authentication-manager to use a custom userDetailsService -->
<security:authentication-manager>
     <security:authentication-provider user-service-ref="customUserDetailsService">
       <security:password-encoder ref="passwordEncoder"/>
     </security:authentication-provider>
 </security:authentication-manager>

 <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
 <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"   id="passwordEncoder"/>

<!-- A custom service where Spring will retrieve users and their corresponding access    levels  -->
<!-- bean id="customUserDetailsService"    class="com.metmi.mmasgis.tutorial.service.CustomUserDetailsService"/-->

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml
                     /WEB-INF/spring/spring-security.xml
        </param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>mmasgisServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
            </param-value>

        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mmasgisServlet</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>

    <filter> 
 <filter-name>springSecurityFilterChain</filter-name> 
 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
 </filter> 


    <filter-mapping> 
 <filter-name>springSecurityFilterChain</filter-name> 
 <url-pattern>/*</url-pattern> 
 </filter-mapping> 

</web-app>

运行应用程序我收到此错误:Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0];

在这里,您可以看到完整的跟踪:http://pastebin.com/qfqq6Vdy

1 个答案:

答案 0 :(得分:1)

根据日志,似乎没有定义id为'customUserDetailsS​​ervice'的bean。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customUserDetailsService'

我看到你发布的配置确实有一个名为customUserDetailsS​​ervice的bean,所以我会检查一些事情。

  • 首先确保您发布的配置与实际配置相符。
  • 配置在单个文件中具有安全配置和UserDetailsS​​ervice。这实际上是你配置它的方式吗?
  • 您使用的是Global Method Security吗?如果是这样的话,已知这会通过与认证相关的bean的急切实例化来触发错误。为了解决这个问题,我还需要看看这个配置。
  • 随机尝试在安全配置的其余部分之前移动customUserDetailsS​​ervice bean。这真的不重要,但更多的是一个实验。