Java 上下文初始化失败,Spring3安全性使用JDBC!

Java 上下文初始化失败,Spring3安全性使用JDBC!,java,spring,jdbc,spring-mvc,spring-security,Java,Spring,Jdbc,Spring Mvc,Spring Security,错误上下文初始化失败,Spring3安全性使用JDBC 文件:applicationContext安全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="h

错误上下文初始化失败,Spring3安全性使用JDBC

文件:
applicationContext安全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>
以下是错误:

[错误,ContextLoader]上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.ProviderManager#0”的bean时出错:设置bean属性“parent”时无法创建[org.springframework.security.config.authentication.AuthenticationManagerFactoryBean]类型的内部bean(内部bean);嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“(内部bean)”的bean时出错:FactoryBean在创建对象时引发异常;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authenticationManager”的bean时出错:在使用键[0]设置bean属性“providers”时,无法解析对bean“org.springframework.security.authentication.dao.DaoAuthenticationProvider”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.dao.DaoAuthenticationProvider#0”的bean时出错:设置bean属性“userDetailsService”时无法解析对bean“org.springframework.security.provisioning.JdbcUserDetailsManager#0”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.provisioning.JdbcUserDetailsManager#0”的bean时出错:设置bean属性“dataSource”时无法解析对bean“dataSource”的引用;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为“dataSource”的bean


这很容易回答。异常消息(在末尾)说:“没有定义名为'dataSource'的bean”。果然,您的
applicationContext security JDBC.xml
文件没有包含
dataSource
bean的定义


SpringSecurity手册的第6.2节包含了关于如何为JDBC用户详细信息服务配置数据源的资料。(提示:SpringSecurity需要被告知使用什么数据库、什么用户名和什么密码…)

我有:在servlet.xml中,如果我将它从sevlet.xml移动到applicationContext-sercurity-jdbc.xml,我会得到以下xml错误:在这一行找到多个注释:-cvc complex type.2.4.c:匹配的通配符是严格的,但找不到元素“bean”的声明。-元素Stuttering John的开始标记:您必须修复名称空间:
..
@Stuttering John-发生这种情况是因为您没有使用正确的名称空间前缀。
applicationContext security JDBC.xml
文件使用
security
作为默认的xml名称空间,因此元素需要显式前缀;e、 g.
。(这些都是“基本XML 101”内容。Spring/SpringSecurity文档假定您了解XML名称空间的工作原理。)@Stephen C更好,但我没有工作,我不得不删除以下几行:现在它无法登录。请告诉我如何添加以下行
<?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>