使用Jaxb2Marshaller进行安全的XML解析

时间:2014-02-10 15:26:51

标签: java xml unmarshalling jaxb2

我需要实现xml解析,以防止XXE和实体扩展攻击。似乎这是一个可以管理这两个问题的代码:

JAXBContext jc = JAXBContext.newInstance(MyClass.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StringSource(body));
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyClass myObject = (MyClass) unmarshaller.unmarshal(xsr);

我希望对Jaxb2Marshaller做同样的事情。这个unmarshaller已经在项目中使用了,我能够想到@Autowire注释。另外classesToBeBound已在applicationContext.xml中设置,因此我不需要为每个动作创建JAXBContext。

有人可以帮我这个吗?

0 个答案:

没有答案