org.xml.sax.SAXNotRecognizedException: 属性 'http://javax.xml.XMLConstants/property/accessExternalDTD' 无法识别

org.xml.sax.SAXNotRecognizedException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized

我想在我的项目中防止 XXE 攻击。它是在 java 7(no maven)和 jboss-as-7 服务器上运行的旧 api 项目。但是在执行过程中我得到了错误: org.xml.sax.SAXNotRecognizedException: 属性 'http://javax.xml.XMLConstants/property/accessExternalDTD' 无法识别.

 org.xml.sax.SAXNotRecognizedException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.

15:19:02,845 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.xerces.jaxp.validation.ValidatorImpl.setProperty(ValidatorImpl.java:218)

15:19:02,846 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at com.uid.kua.web.KUARestController.authenticateAtAUA(KUARestController.java:118)

15:19:02,847 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

15:19:02,847 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

15:19:02,848 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

15:19:02,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at java.lang.reflect.Method.invoke(Method.java:606)

我搜索了一下,每个论坛都有不同的含义,说这是一个错误。我还没有找到针对此异常的任何相关解决方案。请帮忙。提前致谢。

终于解决了。如果这对任何人有帮助,我都会发布答案。 通过在线解决方案后,我无法检测到导致上述错误的主要问题。 为了防止 xxe,我们需要一些已定义的属性,例如: XMLConstants.ACCESS_EXTERNAL_DTD XMLConstants.ACCESS_EXTERNAL_SCHEMA

我们需要 xercesjaxp-api jar 来传递 xml 并防止由api 通过设置上述一些属性来解析 xml。在 JDK 7 之前,这些已经包含在 JDK 7 及更高版本中。所以我们不需要在我们的项目类路径中导入上面的jar。

在我的例子中,我使用 jboss-as-7.1.1.Final 作为应用程序服务器,它也有自己的 xerces jar (org.apache.xerces.)。但是 java 也带有它自己的 xerces 和 jaxp 实现 (com.sun.xerces.)。因此,在部署 war 时,我们会收到上述错误,因为这两个 jar 相互冲突,其中 jboss 加载它自己的 xerces jar。

解法: 我们需要通过在 jboss/org/apache/xerces/main/modules.xml 文件中进行更改来 ​​ 排除 jboss xerces 实现。注释掉如下所示的行:

> <module xmlns="urn:jboss:module:1.1" name="org.apache.xerces">    
> <!--
>     <resources>
>         <resource-root path="xercesImpl-2.9.1-jbossas-1.jar"/>
>         Insert resources here
>     </resources>
> -->
>     <dependencies>
>         <module name="javax.api"/>
>     </dependencies>
> 
> </module>

现在部署并运行您的应用程序。 编码愉快。

似乎在 JAXP 1.5 中引入了属性 accessExternalDTDaccessExternalSchema。但是,Java EE 6(甚至 Java EE 8)仅随 JAXP 1.4 提供。

在我的例子中(运行 在 WildFly 19 和 AdoptOpenJDK 11 上)我能够获得 JDK 的 DocumentBuilderFactory[= 的默认实例24=] 和 SchemaFactory 通过使用它们在 Java 中引入的静态 newDefaultInstance() 方法 9. 在 Java 8 两个 classes 还有一个 newInstance 方法,您可以在其中指定 class 名称,即 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImplcom.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.

此解决方案避免更改 JBoss 模块。

很好的回答 - 这只是代码示例(使用 java 11 和 jboss 7.3):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
 DocumentBuilder builder = dbf.newDocumentBuilder();

 ByteArrayInputStream input = new ByteArrayInputStream(xmlDocument.getBytes(encoding));
 org.w3c.dom.Document doc = builder.parse(input);
 DOMBuilder domBuilder = new DOMBuilder();

 Document docJdom = domBuilder.build(doc);

在我的例子中,我遇到了类似的问题,但与 SAXParser 有关,当设置道具时:XMLConstants.ACCESS_EXTERNAL_DTDXMLConstants.ACCESS_EXTERNAL_SCHEMA

迈克尔建议的解决方案是指定 SAXParserFactory 使用的类名(我使用的是 JDK 8)

final SAXParserFactory factory = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", ClassLoader.getSystemClassLoader());

感谢有关 xerces 的提示。

在 Maven 构建(mvn 验证)期间 运行 Java 测试时我的错误消息如下所示:

Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.

我是这样解决的:

mvn dependency:tree

正在查找继承 xerces 的依赖项:

[INFO] +- au.com.dius.pact:consumer:jar:4.2.9:test
[INFO] |  +- au.com.dius.pact.core:model:jar:4.2.9:test
[INFO] |  |  +- org.apache.commons:commons-collections4:jar:4.1:test
[INFO] |  |  +- com.github.mifmif:generex:jar:1.0.2:test
[INFO] |  |  +- javax.mail:mail:jar:1.5.0-b01:test
[INFO] |  |  +- org.apache.tika:tika-core:jar:1.27:test
[INFO] |  |  \- io.ktor:ktor-http-jvm:jar:1.3.1:test
[INFO] |  |     +- org.jetbrains.kotlinx:atomicfu:jar:0.14.1:test
[INFO] |  |     +- org.jetbrains.kotlinx:kotlinx-coroutines-core:jar:1.3.3:test
[INFO] |  |     +- org.jetbrains.kotlinx:kotlinx-coroutines-core-common:jar:1.3.3:test
[INFO] |  |     \- io.ktor:ktor-utils-jvm:jar:1.3.1:test
[INFO] |  |        \- io.ktor:ktor-io-jvm:jar:1.3.1:test
[INFO] |  +- au.com.dius.pact.core:matchers:jar:4.2.9:test
[INFO] |  |  +- xerces:xercesImpl:jar:2.12.0:test

并排除了Maven中相应Dependency中的xerces Pom.xml:

       <dependency>
            <groupId>au.com.dius.pact</groupId>
            <artifactId>consumer</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>aws-java-sdk-s3</artifactId>
                    <groupId>com.amazonaws</groupId>
                </exclusion>                
                <exclusion>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

javax.xml 通过几个选项加载某些工厂接口的实现。第一个选项是通过系统 属性 配置默认实现。例如:

javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema = com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory
javax.xml.parsers.DocumentBuilderFactory = com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

第二个选项是使用你必须包含在你的 jre 中的 jaxb.properties 文件,或者 jdk 你如何做到这一点在此处描述:https://docs.oracle.com/javase/tutorial/jaxp/properties/usingProps.html

第三个选项是在 META-INF/services 中添加一个文件,该文件的名称应该是您要为其定义实现的 Factory 接口,该文件应该包含完整的包名称 + class 实现的名称。例如,您可以添加一个名为 javax.xml.parsers.DocumentBuilderFactory 的文件,其中包含 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

第四个选项是select默认实现。在 Wildfly/Jboss 的情况下,他们包含了一个仅支持 JAXP 1.4 的 org.apache.xerces 实现。这意味着它将 select 这个实现,这就是你得到错误的原因。

要找出用于您的特定功能的实现,您可以添加一个系统 属性

jaxp.debug = 1

此 属性 将为 jaxp 启用调试模式,您将看到以下输出:

JAXP: using thread context class loader (ModuleClassLoader for Module "deployment.css-private-build-1.0-SNAPSHOT.ear.css-private.war" from Service Module Loader) for search
JAXP: Looking up system property 'javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema'
JAXP: The value is 'com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory'
JAXP: createInstance(com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory)
JAXP: loaded com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory from (no code source)
JAXP: factory 'com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory' was found for http://www.w3.org/2001/XMLSchema

因此,在出现错误之前,您将看到 JAXP 调试日志记录,然后您应该知道必须为哪个工厂提供实现才能使其正常工作。