【问题标题】:Spring Security cannot locate NamespaceHandlerSpring Security 找不到 NamespaceHandler
【发布时间】:2015-12-01 11:37:00
【问题描述】:

在尝试设置 Spring Security 时,我总是遇到这个我仍然不知道如何处理的错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:无法从相对位置导入 bean 定义 [security.xml]
违规资源:ServletContext资源[/WEB-INF/applicationContext.xml];嵌套异常是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/security]
违规资源:ServletContext资源[/WEB-INF/security.xml]

通过编码,我严格遵循Spring Securitydocumentation。在 SO,我也搜索了类似的问题,但没有一个能解决我的问题。

据我所知,错误主要是由两件事引起的: 首先,Spring 无法导入 security.xml,其次,Spring 无法为 XML 模式命名空间定位其 NamespaceHandler。 我是否缺少某些依赖项,或者为什么 Spring 找不到它的 NamespaceHandler

我正在使用 Spring 4.2.0.RELEASESpring Security 4.0.2.RELEASE

所以,这是我的代码:

pom.xml

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>4.0.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>4.0.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>4.0.2.RELEASE</version>
</dependency>

security.xml

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

    <http>
        <intercept-url pattern="/**" access="hasRole('ADMIN')" />
        <form-login />
        <logout />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="ROLE_ADMIN" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

web.xml

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.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>

而在 applicationContext.xml 我只是导入 security.xml

<import resource="security.xml" />

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    您是否也可以将以下内容添加到您的 pom.xml 中:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    

    编辑:

    在你的 web.xml 中的 ContextLoaderListener 声明下面添加这个:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>
    

    【讨论】:

    • 感谢您的回复;我刚刚添加了两个依赖项,但没有任何改变......仍然出现同样的错误。我不明白的是,我的库中确实存在所需的 spring-security-config.jar 但仍然得到了 BeanDefinitionParsingException。
    • @MaxWell 我编辑了我的答案,你可以试试这个新建议吗
    • 再次感谢...不起作用...但现在我得到了一些额外的错误信息:Offending resource: ServletContext resource [/WEB-INF/security.xml].
    • 我刚刚注意到您在 security.xml 命名空间中有 'springframework.org/schema/beans/spring-beans-3.0.xsd' 但您使用的是 spring 4.0.2 和 spring-security4.0.2 不是吗?它可能与您遇到的这个问题有关,除了我不确定这里有什么问题。我还通过从 web.xml 中删除 'classpath:security.xml' 再次编辑了我的答案,因为 applicationContext.xml 将 security.xml 作为资源导入
    • 引用 spring-beans-3.0 可能会导致冲突,因为您的 pom/library 文件夹中有 spring 4 jar,因此请尝试在安全命名空间中不声明版本号,即遵循与你有 spring-security 命名空间声明)
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2015-12-23
    • 2020-01-02
    • 2011-11-03
    • 2013-02-13
    • 2011-03-21
    相关资源
    最近更新 更多