Java 如何使用JAXB2.0禁用DTD获取
我试图使用JAXB来解析一些XML,这些XML是我首先使用xjc创建的。我不想对解组进行任何验证,但即使我已经根据JAXB文档使用Java 如何使用JAXB2.0禁用DTD获取,java,validation,jaxb,dtd,Java,Validation,Jaxb,Dtd,我试图使用JAXB来解析一些XML,这些XML是我首先使用xjc创建的。我不想对解组进行任何验证,但即使我已经根据JAXB文档使用u.setSchema(null)禁用了验证,但这并没有阻止在尝试运行并且找不到架构时抛出FileNotFoundException JAXBContext jc = JAXBContext.newInstance("blast"); Unmarshaller u = jc.createUnmarshaller(); u.setSchema(null); return
u.setSchema(null)禁用了验证
,但这并没有阻止在尝试运行并且找不到架构时抛出FileNotFoundException
JAXBContext jc = JAXBContext.newInstance("blast");
Unmarshaller u = jc.createUnmarshaller();
u.setSchema(null);
return u.unmarshal(blast)
通过设置apache属性
http://apache.org/xml/features/validation/schema
到false
,但我无法让解组器使用我自己的sax解析器。您可以直接从javax.xml.transform.sax.SAXSource创建解组器
请参见本页上的示例:
您“只”需要为该SAXSource提供您自己的URIResolver,下面是示例代码,演示了如何获得使用SAX解析器的实现:
import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource(new FileReader("input.xml"));
SAXSource source = new SAXSource(xmlReader, inputSource);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Foo foo = (Foo) unmarshaller.unmarshal(source);
System.out.println(foo.getValue());
}
}
基于@blaise doughan和@aerobiotic的答案,这里有一个对我有效的解决方案:
import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class Demo2 {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyBean.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
spf.setFeature("http://xml.org/sax/features/validation", false);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource(
new FileReader("myfile.xml"));
SAXSource source = new SAXSource(xmlReader, inputSource);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyBean foo = (MyBean) unmarshaller.unmarshal(source);
}
}
以上建议对我来说没有任何效果。。。 我建议这个代码对我有用☺️ 要从xml中删除Dtd…请使用正则表达式 String str=event.getData(); str=str.replaceAll(“>[^])>”,“”) 在回答“如何使用JAXB2.0禁用DTD获取”问题时 @sameer puri链接,回答问题如下: JAXB解组器 由于javax.xml.bind.Unmarshaller解析xml并且不支持任何禁用XXE的标志,因此必须首先通过可配置的安全解析器解析不受信任的xml,生成源对象,然后将源对象传递给解组器。例如:
这对我不起作用,但它们起作用了:parser.setFeature(“,false);setFeature(“,false);该网站解释了如何在任何主要的Java框架上阻止它。似乎功能安全处理是关于entityExpansionLimit的。检查文档的最后一部分。这回答了“如何让解组器使用我自己的sax解析器”的问题,但没有回答“如何使用JAXB2.0禁用DTD获取”的问题。这对我很有效。另外还添加了
spf.setValidating(false)代码>谢谢
//Disable XXE
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
//Do unmarshall operation
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(),
new InputSource(new StringReader(xml)));
JAXBContext jc = JAXBContext.newInstance(Object.class);
Unmarshaller um = jc.createUnmarshaller();
um.unmarshal(xmlSource);