Java和Xerces:找不到属性XMLConstants.ACCESS_EXTERNAL_DTD

yfwxisqw  于 2023-05-05  发布在  Java
关注(0)|答案(4)|浏览(526)

我在这个博客上寻找类似的帖子,但找不到我问题的答案,所以我决定寻求帮助。
我用Java写了一个简单的函数:

public void open(InputStream stream) throws FoliumFatalException {
        try {
            InputSource is = new InputSource(stream);
            DocumentBuilderFactory dfact = DocumentBuilderFactory.newInstance();
            
//            /* OWASP: inhibit access to External Entities */
            dfact.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); 
            dfact.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); 
            
            _doc = dfact.newDocumentBuilder().parse(is);

        } catch (Throwable t) {
            _logger.error(t, t);
            throw new FoliumFatalException("ENG-0017", "Errore di parsing su stream", t);
        }

    }

我的目标是应用这里公开的OWASP标准,但我得到了以下错误:

java.lang.IllegalArgumentException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.
java.lang.IllegalArgumentException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.
    at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(Unknown Source) ~[xercesImpl-2.8.0.jar:?]
    at agora.folium.engine.impl.j2ee.FoliumJ2eeXmlParserImpl.open(FoliumJ2eeXmlParserImpl.java:108) [classes/:?]
    at agora.folium.engine.impl.FoliumAbstractEngine.loadServices(FoliumAbstractEngine.java:268) [classes/:?]
    at agora.folium.engine.impl.j2ee.FoliumJ2eeEngineImpl.startup(FoliumJ2eeEngineImpl.java:110) [classes/:?]
    at agora.folium.engine.Folium.startup(Folium.java:258) [classes/:?]
    at agora.folium.control.impl.j2ee.FoliumActionServlet.init(FoliumActionServlet.java:94) [classes/:?]
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1230) [catalina.jar:7.0.85]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1174) [catalina.jar:7.0.85]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1066) [catalina.jar:7.0.85]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5370) [catalina.jar:7.0.85]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5668) [catalina.jar:7.0.85]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) [catalina.jar:7.0.85]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1015) [catalina.jar:7.0.85]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:991) [catalina.jar:7.0.85]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) [catalina.jar:7.0.85]
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:712) [catalina.jar:7.0.85]
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:2002) [catalina.jar:7.0.85]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_141]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_141]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_141]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_141]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_141]

我正在使用Eclipse Oxygen、Tomcat 7和Java 1.8。

cgh8pdjw

cgh8pdjw1#

javax.xml.XMLConstants.ACCESS_EXTERNAL_DTDJAXP 1.5中定义,但Xerces不支持它。如果您不能删除Xerces依赖项,您应该在Xerces之前向类路径添加另一个实现。
另外,由于JDK包含Xerces的实现,您可以使用System.propertiesconfigureDocumentBuilderFactory返回JDK版本。

System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
        "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
5m1hhzi4

5m1hhzi42#

这个问题是由于类路径中的Xerces/XercesImpl引起的。Xerces不支持ACCESS_EXTERNAL_DTD属性。
解决方案1.如果可能,从类路径中删除xerces jar。
解决方案2.使用JDK默认实现
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(“com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl”,null);

34gzjxbg

34gzjxbg3#

ApacheXerces-J 2.12.0及更早版本实现了旧版本的JAXP,这些版本不支持您尝试设置的任何一个属性。要阻止对外部实体的访问,您可以编写一个EntityResolver(它总是抛出一个SAXException)并向DocumentBuilder注册该EntityResolver。参见此处的文档[1]。
[1]http://xerces.apache.org/xerces2-j/javadocs/API/javax/xml/parsers/DocumentBuilder.html#setEntityResolver(org.xml.sax.EntityResolver)

b09cbbtk

b09cbbtk4#

我们的Java项目是用Maven构建的。当团队决定在管道中集成SonarQube时,我们遇到了类似的问题。SonarQube不会运行您的测试或生成报告。它只导入预先生成的报告”(更多关于here)。在Java/Kotlin/Scala/JVM的情况下,SonarQube需要一些“JaCoCo XML覆盖率报告”。因此,我们必须在pom.xml中添加一个依赖项:

<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.version}</version>
</dependency>

经过一些调整后,我们已经全部启动并运行。但是一些测试失败了,出现“java.lang.IllegalArgumentException:无法识别属性“http://javax.xml.XMLConstants/property/accessExternalDTD”。at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(Unknown Source)..."。
长话短说,解决方案是将xerces排除在外,正如其他答案中所述:

<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.version}</version>
  <exclusions>
    <exclusion>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

相关问题