spring配置文件验证

发布于 2024-11-27 07:56:11 字数 1593 浏览 4 评论 0原文

我在处理 spring 配置文件时注意到了这种行为。
我的配置文件中有 2 个错误:

  • 一个语法(格式正确)错误,例如拼写错误的结束标记(尝试使用“html”标记)
  • 一个“命名空间”(验证)错误(尝试了错误的路径security 命名空间)

由于 xml 无效,我认为运行时环境返回的第一个错误是验证错误。

这不是真的(带有安全性 3.0.2 的 spring 3.0.2)。

它首先返回:

匹配通配符严格,但找不到元素“http”的声明

然后,当我修复第一个错误时,我得到了(我首先期望的):

元素类型“http”必须由匹配的结束标记“”终止

那么关于语法检查可以说什么呢?语法检查的过程似乎不是作为预备步骤执行的,而是检查和验证是“交错的”。首先检查每个标签,然后进行验证。对吗?它是否依赖于实现?

PS:我的错误 xml(请注意模式位置和 http 结束标记中的两个错误):

<?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.0.xsdFIRSTERROR">

<http>
    <form-login />
    <http-basic />
    <logout />
    <intercept-url pattern="/**" access="ROLE_USER" />
</SECONDERRORhttp>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="myuser" password="mysecret" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
 </authentication-manager>

</beans:beans>

I've noticed this behavior in the processing of spring configuration files.
I have 2 errors in my config file:

  • one syntax (well format) error, say a mispelled closing tag (tryed with "html" tag)
  • a "namespace" (validation) error (tryed a wrong path to security namespace)

Since the xml is not valid, i thought that the first error that the runtime environement returned were the validation error.

That's not true (spring 3.0.2 with security 3.0.2).

It first returns:

The matching wildcard is strict, but no declaration can be found for element 'http'

then, when i fixed this first error, i get (what I was expecting as first):

The element type "http" must be terminated by the matching end-tag ""

So what can be said about the syntax check? It seems that the process of syntax check is not performed as a preliminar step, but checking and validation are "interlaced". Every tag is first checked then validated. Is it right? Is it implementation-dependent?

PS: My faulty xml (notice the two errors in schema location and in http closing tag):

<?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.0.xsdFIRSTERROR">

<http>
    <form-login />
    <http-basic />
    <logout />
    <intercept-url pattern="/**" access="ROLE_USER" />
</SECONDERRORhttp>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="myuser" password="mysecret" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
 </authentication-manager>

</beans:beans>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2024-12-04 07:56:11

您是对的,“语法检查”和验证不是两个单独的步骤。 Spring 使用流式 XML 解析器来提高效率。这种解析器通常在到达有问题的元素时准确地识别错误,而不是之前或之后。

You're right that the "syntax check" and validation aren't two separate steps. Spring uses a streaming XML parser for efficiency. That kind of parser typically identifies an error exactly when it reaches the offending element, not before or after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文