这是为了在使用JAXB API时避免XXE攻击。我知道在使用JAXB时,可以覆盖默认的解析机制,可以使用替代的SAX解析器,并设置实体特性以避免XXE攻击。但希望了解默认解析器的确切含义,并在其上设置安全特性。有什么帮助吗?
发布于 2014-08-12 02:51:28
您可以通过在禁用外部实体支持的情况下使用StAX解析器来利用JAXB来执行以下操作:
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));
Customer customer = (Customer) unmarshaller.unmarshal(xsr);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}
}https://stackoverflow.com/questions/25211643
复制相似问题