java安全之xxe
DocumentBuilder基础使用
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
File file = new File("./payload.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
// 根据tag名获取标签,是不是很像js中的getElementByTagName函数
NodeList nodeList = doc.getElementsByTagName("person");
Element element = (Element) nodeList.item(0);
System.out.println("姓名:" + element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("年龄:" + element.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
}
}
javax.xml.parsers.DocumentBuilder 案例2

防御方法

参考
Last updated