If you use document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
you get an empty nodeList and the source code provided throws your new SecurityException("Cannot find signature")
.
If you document.getElementsByTagName("Signature")
, the Signature element is found but without namespace awareness, throwing this exception later on:
javax.xml.crypto.MarshalException: Document implementation must support DOM Level 2 and be namespace aware
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:189)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:150)
When researching the term getElementsByTagNameNS(XMLSignature.XMLNS, "Signature")
I found code examples where they activate namespace awareness on the new instance of DocumentBuilderFactory manually:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
This should fix your new SecurityException("Cannot find signature")
issue.
Regarding your previous question you not only migrated Java 8 to 11, but also CDI API, OpenWebBeans, Apache Tomcat and maybe other modules too, someone else must have done this activation of namespace awareness for you previously.
Or saying it differently - you @Injected a namespace aware DocumentBuilderFactory instance.
Edit:
For manual configuration and injection of DocumentBuilderFactory
, create a method that @Produces
an instance of the DocumentBuilderFactory
and provides that instance for injection elsewhere:
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.xml.parsers.DocumentBuilderFactory;
@Dependent
public class DocumentBuilderFactoryProducer {
@Produces
@ApplicationScoped
public DocumentBuilderFactory createDocumentBuilderFactory() {
DocumentBuilderFactory result = DocumentBuilderFactory.newInstance();
result.setNamespaceAware(true);
// other customizations ...
return result;
}
}
This can be injected into other beans like so:
@Inject
private DocumentBuilderFactory documentBuilderFactory;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…