Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

java - Set feature accessExternalDTD in TransformerFactory

For security reasons I'm adding the statement:

transformerFactory.setFeature("http://javax.xml.XMLConstants/property/accessExternalDTD", false);

However I'm getting the following error in the log files:

25-Nov-2014 09:35:48.802 SEVERE [http-nio-8080-exec-14] CIMObject.CIMObjectCollectDataHANA.setRunningXML TransformerConfigurationException setRunningXMLHANA: javax.xml.transform.TransformerConfigurationException: Cannot set the feature 'http://javax.xml.XMLConstants/property/accessExternalDTD' on this TransformerFactory.

Someone experienced the same error and was able to fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It appears the problem is that such feature is not defined in com/sun/org/apache/xalan/internal/utils/FeatureManager.java.

If you are using java 8, then what you need to do is simply to call:

transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

This is because in com/sun/org/apache/xalan/internal/xsltctrax/TransformerFactoryImpl.java:

if (value && XalanConstants.IS_JDK8_OR_ABOVE) {
    _xmlSecurityPropertyMgr.setValue(
         Property.ACCESS_EXTERNAL_DTD, 
         State.FSP, 
         XalanConstants.EXTERNAL_ACCESS_DEFAULT_FSP
    ); // ACCESS_EXTERNAL_DTD is disabled by setting to the default value
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...