Spring 3.0 - 找不到用于XML模式命名空间[http://www.springframework.org/schema/security]的Spring NamespaceHandler

181

您有什么想法,可能是什么原因导致这个问题出现?

无法定位 Spring NamespaceHandler 的 XML schema 命名空间, [http://www.springframework.org/schema/security]

org.springframework.web.context.ContextLoader initWebApplicationContext: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]

这是我的applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.xsd">
...
</beans:beans>

在我的pom.xml文件中,我有以下内容:

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

我在跟随Spring pizzashop教程时遇到了这个问题。 - Rob Grant
这是你完整的pom.xml吗?因为你很可能缺少一个jar包。 - Marco Schoolenberg
15个回答

287

我需要添加一个额外的Maven依赖:

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

3
感谢解决我的问题。有关Spring Security 3.0代码库重组的更多信息,请访问:http://blog.springsource.com/2009/06/03/spring-security-300m1-released/。 - Rydell
不错的链接。几个月前我也可能用得上它。 - Taylor Leese
12
培根再次被SO所救! - Andrew Swan
当尝试仅使用spring-security-cas JAR时,类似的解决方案适用。 - Ryan Ransford
我推荐这个链接给那些遇到Unable to locate Spring NamespaceHandler for XML schema namespace [xxxxx]问题的人。我以前也遇到过类似的问题,这个链接对我帮助很大! - Cotta

18

我在尝试部署应用程序时遇到了相同的错误消息。在Spring中,安全配置xml文件可以与applicationContext.xml不同,通常位于WEB-INF文件夹中的applicationContext-security.xml中。需要应用的更改是针对web.xml文件的。

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

应用程序上下文文件(applicationContext.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.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http auto-config='true'>
        <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page='login.jsp'/>
    </http>

</beans:beans>
即使您进行了这些更改,命名空间错误仍将存在。为了摆脱这个问题,请将以下jar文件添加到WEB-INF / lib,然后添加到库中:
  • spring-security-acl-3.1.0.M2.jar
  • spring-security-config-3.1.0.M2.jar
  • spring-security-core-3.1.0.M2.jar
  • spring-security-taglibs-3.1.0.M2.jar
  • spring-security-web-3.1.0.M2.jar

你几乎必须使用Maven来启动Spring。即使你不情愿地这样做,它仍然无法正常工作!某个人在某个地方正在大笑……这个答案帮助我稍微减轻了一点挫败感。 - Arne Evertsson

12

8
我使用了 spring-security-config jar,它帮助我解决了这个问题。

6
解决方案明确是“spring-security-config”,而不在你的WEB-INF/lib中。
对于我在Eclipse中使用Maven的项目,事实证明并非所有maven依赖项都被复制到WEB-INF/lib。查看项目 -> 属性 -> 部署装配,只有一些文件被复制。
为了解决这个问题,我点击“添加”,然后“Java构建路径条目”,最后是“Maven依赖项”。
我已经在SO和网络上搜寻了一个小时,希望这能帮助其他人。

3

@James Jithin - 当你的beans和security schema在xsi:schemaLocation中有两个不同的版本时,也会出现这样的异常。这是你贴出的片段中的情况:

xsi:schemaLocation="http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
 http://www.springframework.org/schema/security  
 http://www.springframework.org/schema/security/spring-security-3.1.xsd"

在我的情况下,将它们都更改为3.1解决了问题。

我刚刚成功地使用以下内容使其工作:http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/security/spring-security-3.2.xsd在我的情况下,我缺少了“spring-security-config” jar包。 - Ithar
同意这个评论。我的问题也是由于这个原因引起的。 - DolphinJava

3

Spring网站上有一个不错的Maven依赖列表,其中需要的主要构件包括:

  1. spring-security-core
  2. spring-security-web
  3. spring-security-config

2

我做了什么:

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

并且

xsi:schemaLocation="
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

运行完美。更多请参考Baeldung


0

我在部署到Virgo时遇到了这个错误。解决办法是将这个东西添加到我的bundle导入中:

org.springframework.transaction.config;version="[3.1,3.2)",

我注意到在META-INF文件夹下的Spring JAR包中有一个spring.schemas和spring.handlers部分,而且它们指向的类(在这种情况下是org.springframework.transaction.config.TxNamespaceHandler)必须被导入。


0

几分钟前我也遇到了同样的问题,我的部署程序中缺少了'Maven依赖项'库。 我通过Eclipse中的“Web部署程序”部分添加了它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接