|
| 1 | +package org.joychou.config; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.core.io.ClassPathResource; |
| 6 | +import org.w3c.dom.Document; |
| 7 | +import org.w3c.dom.Node; |
| 8 | +import org.w3c.dom.NodeList; |
| 9 | + |
| 10 | +import javax.xml.parsers.DocumentBuilder; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 12 | +import java.io.File; |
| 13 | +import java.util.ArrayList; |
| 14 | + |
| 15 | +public class SafeDomainParser { |
| 16 | + |
| 17 | + private static Logger logger= LoggerFactory.getLogger(SafeDomainParser.class); |
| 18 | + |
| 19 | + public SafeDomainParser(){ |
| 20 | + |
| 21 | + String safeTag = "safedomain"; |
| 22 | + String domainSafeTag = "domain"; |
| 23 | + String safeDomainClassPath = "url" + File.separator + "safe_domain.xml"; |
| 24 | + ArrayList<String> safeDomains = new ArrayList<>(); |
| 25 | + |
| 26 | + try { |
| 27 | + // 读取resources目录下的文件 |
| 28 | + ClassPathResource resource = new ClassPathResource(safeDomainClassPath); |
| 29 | + File file = resource.getFile(); |
| 30 | + |
| 31 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 32 | + DocumentBuilder db = dbf.newDocumentBuilder(); |
| 33 | + Document doc = db.parse(file); // parse xml |
| 34 | + |
| 35 | + NodeList rootNode = doc.getElementsByTagName(safeTag); |
| 36 | + Node domainsNode = rootNode.item(0); |
| 37 | + NodeList child = domainsNode.getChildNodes(); |
| 38 | + |
| 39 | + for (int i = 0; i < child.getLength(); i++){ |
| 40 | + Node node = child.item(i); |
| 41 | + if (node.getNodeName().equals(domainSafeTag)) { |
| 42 | + safeDomains.add(node.getTextContent()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + }catch (Exception e){ |
| 47 | + logger.error(e.toString()); |
| 48 | + } |
| 49 | + |
| 50 | + WebConfig wc = new WebConfig(); |
| 51 | + wc.setSafeDomains(safeDomains); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
0 commit comments