【问题标题】:Context initialization failed with Spring3 Security using JDBC!使用 JDBC 的 Spring3 Security 上下文初始化失败!
【发布时间】:2011-07-24 03:47:06
【问题描述】:

使用 JDBC 的 Spring3 Security 初始化错误上下文失败。

文件: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] 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.ProviderManager#0”的bean时出错:无法创建类型为[org.springframework.security.config。 authentication.AuthenticationManagerFactoryBean] 同时设置 bean 属性'parent';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“(内部 bean)”的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authenticationManager' 的 bean 时出错:设置时无法解析对 bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 的引用带有键 [0] 的 bean 属性“提供者”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.provisioning.JdbcUserDetailsManager# 的​​引用0' 同时设置 bean 属性 'userDetailsS​​ervice';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“org.springframework.security.provisioning.JdbcUserDetailsManager#0”的 bean 时出错:设置 bean 属性“dataSource”时无法解析对 bean“dataSource”的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

【问题讨论】:

    标签: java spring jdbc spring-mvc spring-security


    【解决方案1】:

    这很容易回答。异常消息说(最后)“没有定义名为'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' 的声明。 - 元素 的开始标签
    • 口吃的约翰:你必须修复命名空间:&lt;beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; ...
    • @Stuttering John - 发生这种情况是因为您没有使用正确的命名空间前缀。 applicationContext-security-JDBC.xml 文件使用 security 作为默认 XML 命名空间,因此 元素需要显式前缀;例如&lt;beans:bean ...&gt;。 (这都是“基本 XML 101”的内容。Spring / SpringSecurity 文档假定您了解 XML 命名空间的工作原理。)
    • @Stephen C 更好,但我但没有工作我不得不删除以下行: 现在它无法登录。请告诉我如何添加以下行
    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 2014-09-13
    • 2015-09-09
    • 2011-11-23
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多