【问题标题】:Error creating bean with name 'org.springframework.security.filterChains创建名为 'org.springframework.security.filterChains 的 bean 时出错
【发布时间】:2015-03-08 21:33:39
【问题描述】:

这是我的 dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
                      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>



 <context:component-scan base-package="com.controller"/>
<mvc:annotation-driven/>

<bean id="student_dao" class="com.dao.User_dao_imp"/>
<bean id="student_service" class="com.service.User_service_imp"/>
<bean id="product_dao" class="com.dao.Product_dao_impl"/>
<bean id="product_service" class="com.service.Product_service_impl"/>

<bean id="userdetail" class="com.controller.UserDetail" />


<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
 <property name="basename" value="/WEB-INF/config/messages"/>
 <property name="defaultEncoding" value="UTF-8"/>         
</bean>



<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />



 </beans>

这是我的 security.xml

     <?xml version="1.0" encoding="UTF-8"?>


<beans:beans xmlns="http://www.springframework.org/schema/security"
 xmlns:beans="http://www.springframework.org/schema/beans"
 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.2.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http use-expressions="true">
<intercept-url pattern="/add_role.htm" access="hasRole('ROLE_ADMIN')" />
<form-login login-page="/login.htm"/>
<logout logout-url="/logout"/>
 </http>


<authentication-manager>
<authentication-provider user-service-ref="userdetail">
    <password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>

</beans:beans>

调试后出现错误

严重:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:开始:org.apache.catalina.LifecycleException:org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework”的bean时出错.security.filterChains':使用键 [0] 设置 bean 属性“sourceList”时无法解析对 bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.web.DefaultSecurityFilterChain#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter# 的​​引用0' 同时使用键 [3] 设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.authentication.ProviderManager# 的​​引用0' 同时设置 bean 属性 'authenticationManager';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authentication.ProviderManager#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean# 的引用0' 同时设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' 的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authenticationManager' 的 bean 时出错:设置时无法解析对 bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 的引用带有键 [0] 的构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.dao.DaoAuthenticationProvider#0”的 bean 时出错:设置 bean 属性“userDetailsS​​ervice”时无法解析对 bean“userdetail”的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userdetail' is defined

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    security.xml 配置中定义您的userdetail bean,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
     xmlns:beans="http://www.springframework.org/schema/beans"
     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.2.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd">
    
    <http use-expressions="true">
    <intercept-url pattern="/add_role.htm" access="hasRole('ROLE_ADMIN')" />
    <form-login login-page="/login.htm"/>
    <logout logout-url="/logout"/>
     </http>
    
    
    <authentication-manager>
    <authentication-provider user-service-ref="userdetail">
        <password-encoder hash="bcrypt" />
    </authentication-provider>
    </authentication-manager>
    
    <beans:bean id="userdetail" class="com.controller.UserDetail" />
    </beans:beans>
    

    【讨论】:

    • 已定义,其他问题
    • 我知道你在dispatcher-servlet.xml 中定义了它,但不知何故弹簧容器无法识别它。将userdetail bean 定义从dispatcher-servlet.xml 复制到security.xml
    • 仔细查看堆栈跟踪:NoSuchBeanDefinitionException: No bean named 'userdetail' is defined
    猜你喜欢
    • 2012-03-28
    • 2012-07-11
    • 2013-08-28
    • 2015-09-14
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    相关资源
    最近更新 更多