2011-03-28 84 views
2

使用JDBC的Spring3安全性錯誤上下文初始化失敗。使用JDBC的Spring3安全性上下文初始化失敗!

文件:applicationContext-security-JDBC.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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 


    <http auto-config="true"> 
     <intercept-url pattern="/friends/**" access="ROLE_USER" /> 
     <intercept-url pattern="/articles/**" access="ROLE_USER" /> 
     <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
    </http> 

    <authentication-manager alias="authenticationManager"> 

     <authentication-provider> 
      <jdbc-user-service data-source-ref="dataSource" /> 
     </authentication-provider> 
    </authentication-manager> 
</beans:beans> 

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"> 

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
     /WEB-INF/applicationContext-security-JDBC.xml 
     </param-value> 
    </context-param> 

    <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> 


    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/log4j.properties</param-value> 
    </context-param> 



    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 



    <!-- 
     - Loads the root application context of this web app at startup. 
     - The application context is then available via 
     - WebApplicationContextUtils.getWebApplicationContext(servletContext). 
    --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- 
     - Publishes events for session creation and destruction through the application 
     - context. Optional unless concurrent session control is being used. 
     --> 
    <listener> 
     <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
    </listener> 

    <listener> 
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 


</web-app> 

下面是錯誤:

[ERROR,ContextLoader] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.config.authentication.AuthenticationManagerFactoryBean] while setting bean property 'parent'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting bean property 'providers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'org.springframework.security.provisioning.JdbcUserDetailsManager#0' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.provisioning.JdbcUserDetailsManager#0': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

回答

2

,很容易回答。異常消息說(最後)「沒有定義名爲'dataSource'的bean」。當然,您的applicationContext-security-JDBC.xml文件不包含dataSource bean的定義。

SpringSecurity手冊的第6.2節包含有關如何爲JDBC用戶詳細信息服務配置數據源的內容。 (提示:SpringSecurity必須告訴什麼數據庫,使用什麼用戶名和密碼是什麼...)

+0

我: \t的 \t \t <屬性名=」 driverClassName」值= 「$ {database.driver}」/> \t \t <屬性名= 「URL」 值= 「$ {database.url}」/> \t \t \t \t <屬性名= 「密碼」 的值= 「$ {database.password}」/> \t 的servlet.xml中 – SJS 2011-03-28 02:41:16

+0

內如果我從sevlet.xml到的applicationContext-sercurity-jdbc.xml中會收到移動它以下xml錯誤:在此行發現多個註釋: \t - cvc-complex-type.2.4.c:匹配的通配符是嚴格的,但對於元素'bean'沒有聲明。 \t - 開始元素 SJS 2011-03-28 03:23:23

+0

口吃約翰的標籤:您必須解決的命名空間:'<豆類:豆類ID = 「數據源」 類= 「org.springframework.jdbc.datasource.DriverManagerDataSource」> ...' – 2011-03-28 07:03:55

相關問題