【问题标题】:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/security]
【发布时间】:2011-11-03 13:27:55
【问题描述】:

我正在开发我的第一个 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 出现以下错误:

严重:上下文初始化失败
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)

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

编辑 我尝试添加 3.0.2 版本的 Spring-Security 并得到了这个:

上下文初始化失败
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

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    您的类路径中需要spring-security-config.jar

    这个异常意味着security: xml 命名空间不能被spring“解析器”处理。它们是NamespaceHandler 接口的实现,因此您需要一个知道如何处理&lt;security: 标记的处理程序。那是位于spring-security-configSecurityNamespaceHandler

    【讨论】:

    • 请注意“spring-secuirty-config.jar”中的错字。我确实尝试过编辑它,但 SO 不允许如此短的更改。 (WTH?)
    • 对于你需要的maven:org.springframework.securityspring-security-config${spring.security.version }
    • 在我的例子中,将 spring-security jar 文件添加到 WEB-INF/lib,这解决了问题。
    • 如果使用 eclipse,让我印象深刻的一件事是,有时需要在创建 jar 时打包 jar 而不是提取。忘记选择此选项时也会出现此错误。
    【解决方案2】:

    我遇到了同样的问题。唯一解决它的方法是将每个 spring jar 文件的 META-INF/spring.handler 和 META-INF/spring.schemas 的内容合并到我的 META-INF 项目下的相同文件名中。

    这两个线程解释得更好:

    【讨论】:

    • 通过将我的 META-INF/spring.handler 替换为您第一个链接中引用的解决方案:blog.idm.fr/2009/09/…
    • 唉,你的两个引用都死了。
    • 对不起@Gangnus,他们至少 6 岁。也许第一个现在在这里:cnblogs.com/abnercai/p/3253644.html 但我已经失去了这个上下文。
    • 谢谢,对于未来,@Carlos 请注意,根据 SO 规则和常识,最好将引用直接 cntrl+C/v 放入答案,对于这种情况。至于这个问题,我通过将 javax.servlet 作为 maven 依赖项并在 maven 和 IntelliJ 中刷新外部 jar 来解决它。
    【解决方案3】:

    就我而言,这是由 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>
    

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 2012-01-21
      • 2013-11-01
      • 2014-04-19
      • 2015-12-05
      • 1970-01-01
      相关资源
      最近更新 更多