spring配置文件验证
我在处理 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是对的,“语法检查”和验证不是两个单独的步骤。 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.