2012-03-28 29 views
19

Sto provando a usare JAXB per smascherare un po 'di XML che ho usato xjc per creare in primo luogo. Non voglio fare alcuna convalida sullo smantellamento, ma anche se ho disabilitato la convalida secondo la documentazione JAXB con u.setSchema(null);, ma questo non ha impedito di lanciare uno FileNotFoundException quando tenta di eseguire e non riesce a trovare il schema.Come disabilitare il recupero DTD usando JAXB2.0

JAXBContext jc = JAXBContext.newInstance("blast"); 
Unmarshaller u = jc.createUnmarshaller(); 
u.setSchema(null); 
return u.unmarshal(blast) 

Ho visto domande simili per la disattivazione SAX parsing di convalida impostando la proprietà apache http://apache.org/xml/features/validation/schema-false, ma non riesco a ottenere l'unmarshaller usare il mio parser SAX.

risposta

7

Di seguito è riportato il codice di esempio che illustra come ottenere un'implementazione JAXB (JSR-222) di utilizzare il SAX parser:

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()); 
    } 

} 
+9

che non ha funzionato per me, ma questi hanno fatto: parser.setFeature ("http: //apache.org/xml/features/nonvalidating/load-external-dtd ", false); parser.setFeature ("http://xml.org/sax/features/validation", false); – aerobiotic

+0

aerobiotic è corretto – sura2k

+0

[Questo] (https://www.owasp.org/index.php/XML_External_Entity_ (XXE) _Prevention_Cheat_Sheet # SAXTransformerFactory) sito Web spiega come può essere bloccato su uno dei principali framework Java. –

15

Basandosi sulle risposte da @ Blaise-Doughan e @aerobiotic, ecco una soluzione che ha funzionato per me:

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); 
    } 
} 
+0

Questo ha funzionato per me.Inoltre aggiunto 'spf.setValidating (false);' Grazie – gihan

Problemi correlati