无法找到Spring命名空间处理程序,用于XML模式命名空间[http://www.springframework.org/schema/security]。

54

我正在开发我的第一个Spring Security应用程序。我的applicationContext-security.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  -  Namespace-based OpenID configuration
  -->

<b:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:b="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.0.xsd">

    <http>
        <intercept-url pattern="/**" access="ROLE_USER"/>
        <intercept-url pattern="/index.xhtml*" filters="none"/>
        <logout/>
        <openid-login login-page="/index.xhtml" authentication-failure-url="/index.xhtml?login_error=true">
            <attribute-exchange>
                <openid-attribute name="email" type="http://schema.openid.net/contact/email" required="true" count="2"/>
                <openid-attribute name="name" type="http://schema.openid.net/namePerson/friendly" />
            </attribute-exchange>
        </openid-login>
        <remember-me token-repository-ref="tokenRepo"/>
    </http>

    <b:bean id="tokenRepo"
            class="org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl" />

    <authentication-manager alias="authenticationManager"/>

    <user-service id="userService">
        <user name="http://user.myopenid.com/" authorities="ROLE_SUPERVISOR,ROLE_USER" />
    </user-service>

</b:beans>

Web.xml文件长这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

    <display-name>Spring Security OpenID Demo Application</display-name>

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

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

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>openid.root</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>

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

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
     <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

应用程序的清理和构建成功,但当我尝试部署应用程序时,Jetty 7会给我以下错误:

SEVERE:上下文初始化失败
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:无法找到XML模式名称空间的Spring NamespaceHandler[http://www.springframework.org/schema/security]
冒犯资源:ServletContext资源[/WEB-INF/applicationContext-security.xml]
在org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
在org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
在org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)

尝试了所有方法但无法解决此错误。任何帮助将不胜感激。

编辑 我尝试添加了Spring-Security的3.0.2版本,并得到以下结果:

上下文初始化失败
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自ServletContext资源[/WEB-INF/applicationContext-security.xml]中XML文档中的第13行无效;
嵌套异常是org.xml.sax.SAXParseException;行号:13;列号:11;cvc-complex-type.2.4.c:匹配通配符是严格的,但没有找到元素'http'的声明。 在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)

3个回答

92
您需要在类路径中添加 spring-security-config.jar
这个异常意味着 spring "解析器" 无法处理 security: 的xml命名空间。它们是 NamespaceHandler 接口的实现,所以您需要一个知道如何处理<security:标签的处理程序。这就是位于 spring-security-config 中的 SecurityNamespaceHandler

请注意'spring-secuirty-config.jar'中的拼写错误。我尝试进行编辑,但是SO不允许如此短的更改。 (真是什么鬼?) - Paul
1
对于Maven,您需要添加以下依赖项:<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> - Justin
在我的情况下,我将Spring Security JAR文件添加到WEB-INF/lib目录中,这解决了问题。 - nikolai.serdiuk
如果使用Eclipse,有一件事情让我感到困扰,那就是在创建JAR文件时有时需要打包JAR文件而不是提取。当忘记选择此选项时,就会出现此错误。 - Andrew Scott Evans

7

通过将我的META-INF / spring.handler替换为您第一个链接中引用的那个,问题得到解决:http://blog.idm.fr/2009/09/maven-assembly-plugin-and-spring-namespace-handlers.html#viewSource - Remi Mélisson
哎呀,你所引用的两个对象都已经失效了。 - Gangnus
抱歉 @Gangnus,它们至少有6年的历史了。也许第一个现在在这里:https://www.cnblogs.com/abnercai/p/3253644.html 但我已经失去了它的上下文。 - Carlos
谢谢,未来请注意,根据SO规则和常识,最好将引用直接复制粘贴到答案中。至于问题,我通过将javax.servlet作为maven依赖项并在maven和IntelliJ中刷新外部jar解决了它。 - Gangnus

0
在我的情况下,这是由于maven-jar-plugin添加的自定义清单条目引起的。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <archive>
            <index>true</index>
            <manifest>
                <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
                <git>${buildNumber}</git>
                <build-time>${timestamp}</build-time>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

删除以下条目解决了问题

<index>true</index>
<manifest>
    <addClasspath>true</addClasspath>
</manifest>

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