【问题标题】:Setting up Spring Security: Invalid content was found starting with element 'security:http'设置 Spring Security:发现以元素 'security:http' 开头的无效内容
【发布时间】:2013-12-28 06:39:21
【问题描述】:

我正在尝试按照 here 配置 spring-security 并且似乎遇到了我对 XML 文件如何工作的理解的限制。

按照指示,我已将 spring 安全性添加到 applicationContext.xml,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:security="http://www.springframework.org/schema/security">

这一步部署得很好。

然后我去web.xml并添加:

<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>

仍然可以正常部署。

然后我尝试添加

<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

我也尝试过

<security:http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</security:http>

在这两种情况下,我在尝试部署应用程序时都看到了错误:

Error occurred during deployment: Exception while deploying the app [OmegaRM] : org.xml.sax.SAXParseException; lineNumber: 53; columnNumber: 39; Deployment descriptor file WEB-INF/web.xml in archive [web].  cvc-complex-type.2.4.a: Invalid content was found starting with element 'security:http'. One of '{"http://xmlns.jcp.org/xml/ns/javaee":module-name, "http://xmlns.jcp.org/xml/ns/javaee":description, "http://xmlns.jcp.org/xml/ns/javaee":display-name, "http://xmlns.jcp.org/xml/ns/javaee":icon, "http://xmlns.jcp.org/xml/ns/javaee":distributable, "http://xmlns.jcp.org/xml/ns/javaee":context-param, "http://xmlns.jcp.org/xml/ns/javaee":filter, "http://xmlns.jcp.org/xml/ns/javaee":filter-mapping, "http://xmlns.jcp.org/xml/ns/javaee":listener, "http://xmlns.jcp.org/xml/ns/javaee":servlet, "http://xmlns.jcp.org/xml/ns/javaee":servlet-mapping, "http://xmlns.jcp.org/xml/ns/javaee":session-config, "http://xmlns.jcp.org/xml/ns/javaee":mime-mapping, "http://xmlns.jcp.org/xml/ns/javaee":welcome-file-list, "http://xmlns.jcp.org/xml/ns/javaee":error-page, "http://xmlns.jcp.org/xml/ns/javaee":jsp-config, "http://xmlns.jcp.org/xml/ns/javaee":security-constraint, "http://xmlns.jcp.org/xml/ns/javaee":login-config, "http://xmlns.jcp.org/xml/ns/javaee":security-role, "http://xmlns.jcp.org/xml/ns/javaee":env-entry, "http://xmlns.jcp.org/xml/ns/javaee":ejb-ref, "http://xmlns.jcp.org/xml/ns/javaee":ejb-local-ref, "http://xmlns.jcp.org/xml/ns/javaee":service-ref, "http://xmlns.jcp.org/xml/ns/javaee":resource-ref, "http://xmlns.jcp.org/xml/ns/javaee":resource-env-ref, "http://xmlns.jcp.org/xml/ns/javaee":message-destination-ref, "http://xmlns.jcp.org/xml/ns/javaee":persistence-context-ref, "http://xmlns.jcp.org/xml/ns/javaee":persistence-unit-ref, "http://xmlns.jcp.org/xml/ns/javaee":post-construct, "http://xmlns.jcp.org/xml/ns/javaee":pre-destroy, "http://xmlns.jcp.org/xml/ns/javaee":data-source, "http://xmlns.jcp.org/xml/ns/javaee":jms-connection-factory, "http://xmlns.jcp.org/xml/ns/javaee":jms-destination, "http://xmlns.jcp.org/xml/ns/javaee":mail-session, "http://xmlns.jcp.org/xml/ns/javaee":connection-factory, "http://xmlns.jcp.org/xml/ns/javaee":administered-object, "http://xmlns.jcp.org/xml/ns/javaee":message-destination, "http://xmlns.jcp.org/xml/ns/javaee":locale-encoding-mapping-list, "http://xmlns.jcp.org/xml/ns/javaee":deny-uncovered-http-methods, "http://xmlns.jcp.org/xml/ns/javaee":absolute-ordering}' is expected.. Please see server.log for more details.

我不明白我在配置中缺少什么以使此错误出现。我尝试如下为标签添加安全性,但没有任何区别:

<web-app version="3.1" 
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:security="http://www.springframework.org/schema/security"
     xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

我确实发现这个问题有同样的错误:

Spring Security beginner's question. Build failed

这里推荐的解决方案是使用我已经尝试过的安全命名空间。

所以我的问题是,是否有人知道我在这里做错了什么?

如果可能的话,我也非常感谢一个链接和/或更一般的解释,说明如何确定 Spring 中的哪些文件中应该包含哪些配置以及它们如何结合在一起!到目前为止,我已经运行了一些应用程序,但我觉得我并没有正确理解我为使它们工作所做的工作!

我已将以下 JAR 添加到项目库中:

【问题讨论】:

  • 你在你的类路径中添加了 Spring Security jars 吗?
  • 我相信是的,看我刚刚添加的截图。

标签: java spring spring-security


【解决方案1】:

您的url-intercept 也需要安全命名空间。

<security:intercept-url pattern="/**" access="ROLE_USER" />

如果您的安全配置在它自己的应用程序配置 xml 中,您可以使用安全作为默认命名空间,从而避免必须在所有内容前面加上 security:。例如:

<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 auto-config='true'>
       <intercept-url pattern="/**" access="ROLE_USER" />
   </http>
   ...
</beans:beans>

然后从您的主应用程序配置中,您可以导入安全配置。例如,假设您的安全配置名称为 security-application-config.xml,您可以像这样导入它。

//in your application-config.xml 
<import resource="security-application-config.xml"/>
<import resource="mongo-application-config.xml"/>

【讨论】:

  • 啊。我有一种感觉,这将是一件简单的事情。如果是这样的话,我会去 headdesk 几次。我现在在家,但我会在早上第一件事尝试。 security.xml 的想法听起来不错,但是一旦我创建了该 xml 文件,我该怎么做才能链接到它?还是被框架自动拾取?
  • 这里要注意的另一件重要事情是,上面示例中的以下架构位置:springframework.org/schema/security/spring-security.xsd 为您提供了最新的架构,而无需 URL 中的版本
  • 这让我走了 95% 的路,我还需要将它从 web.xml 中移出(我认为 spring security 的东西已经说过把它放在那里,尽管回头看看有点模棱两可)到 applicationContext.xml,现在它似乎正在读取配置。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 2013-02-18
  • 2016-01-22
  • 2013-05-10
相关资源
最近更新 更多