创建名为'org.springframework.security.filterChains的bean时出错

时间:2015-01-10 18:20:54

标签: spring spring-security

这是我的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>

调试后我收到错误

SEVERE:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework'的bean时出错.security.filterChains':用key [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#使用键[3]设置构造函数参数时为0';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0'的bean时出错:无法解析对bean的引用'org.springframework.security.authentication.ProviderManager#设置bean属性'authenticationManager'时为0';嵌套异常是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:没有定义名为'userdetail'的bean

1 个答案:

答案 0 :(得分:0)

userdetail配置中定义security.xml 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>