我正在将Java 6中稳定的应用程序更新为Java 8.该应用程序使用SAML身份验证,该身份验证现在在Java更新后的验证期间中断。
我的验证码:
public static boolean isValid(Element target, KeySelector keySelector) {
NodeList nodeList = target.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
Node sigNode = nodeList.item(0);
DOMValidateContext context = new DOMValidateContext(keySelector, sigNode);
XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
try {
XMLSignature signature = factory.unmarshalXMLSignature(context);
if (!signature.validate(context)) { // Throws an XMLSignatureException
....
堆栈跟踪如下:
java.lang.RuntimeException: javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID [5db62480-f1ce-4ea1-be81-b1ebdcb1d9ec]
....
Caused by: javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID [5db62480-f1ce-4ea1-be81-b1ebdcb1d9ec]
at org.jcp.xml.dsig.internal.dom.DOMReference.dereference(DOMReference.java:436) ~[na:1.8.0_20]
at org.jcp.xml.dsig.internal.dom.DOMReference.validate(DOMReference.java:398) ~[na:1.8.0_20]
at com.mycompany.commons.authentication.saml.common.SignatureUtil.isValid(SignatureUtil.java:154) ~[classes/:na]
... 24 common frames omitted
Caused by: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID [5db62480-f1ce-4ea1-be81-b1ebdcb1d9ec]
at org.jcp.xml.dsig.internal.dom.DOMURIDereferencer.dereference(DOMURIDereferencer.java:121) ~[na:1.8.0_20]
at org.jcp.xml.dsig.internal.dom.DOMReference.dereference(DOMReference.java:430) ~[na:1.8.0_20]
... 26 common frames omitted
Caused by: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID [5db62480-f1ce-4ea1-be81-b1ebdcb1d9ec]
at com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverFragment.engineResolveURI(ResolverFragment.java:89) ~[na:1.8.0_20]
at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:300) ~[na:1.8.0_20]
at com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:285) ~[na:1.8.0_20]
at org.jcp.xml.dsig.internal.dom.DOMURIDereferencer.dereference(DOMURIDereferencer.java:114) ~[na:1.8.0_20]
... 27 common frames omitted
我一直在努力调试这个因为我无法进入源代码,但我已经设法缩小了一点。根据这个oracle guide,有两个阶段的验证,即签名和参考。我在参考阶段得到了一个例外。下面的修订更好地隔离了这个问题。
...
XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
try {
XMLSignature signature = factory.unmarshalXMLSignature(context);
List<Reference> references = DOMSignedInfo.getSignedInfoReferences(signature.getSignedInfo());
// A DOMURIReference, its URI matches the value of the ID we are searching for
Reference reference = references.get(0);
// The Reference component was what was breaking in the XMLSignature.validate method
reference.validate(context); // Throws XMLSignatureException
值得注意的是,我可以从上面的Node
组件中获取Reference
,这是一个URI属性,其值与堆栈跟踪中报告的ID匹配,除了它是前缀用“#”。我觉得我越来越近,但即使我可以使这个URI属性成为ID类型,它仍然找不到,因为当ResolverFragment类试图通过ID获取元素时,下游,ID搜索参数没有“#”前缀。
根问题详见:https://bugs.openjdk.java.net/browse/JDK-8017171。在此链接中,注意到以下解决方法选项:
使用验证模式,该模式将使用ID引用注册元素。
在验证签名之前,使用DOMValidateContext.setIdAttributeNS方法注册ID元素。
实现一个自定义URIDereferencer,它可以找到这些引用并使用DOMValidateContext.setURIDereferencer方法覆盖内置的URIDereferencer。
已经提出了类似的问题,例如this question,但要么解决方案对我不起作用,要么我无法将解决方案应用于我的案例。我没有听到有人提到Reference
组件似乎在我面临的问题中发挥了重要作用。
如何解决此问题?请帮忙!
答案 0 :(得分:1)
我的问题是我对Reference
太挂了,对我正在使用的XML文档了解不够。
虽然我没有真正看过XML,但通过调试我可以得出它看起来像这样。
<Response>
<Issuer>
<Status>
<StatusCode>
</Status>
<Assertion ID="THE ID WE NEED!!!!!">
<Issuer>
</Issuer>
<Subject>
<NameID
</Subject>
<Conditions NotBefore=".." NotOnOrAfter="...">
</Conditions
<AttributeStatement>
<Attribute Name="...">
</Attribute>
<Attribute>
</Attribute>
...
</AttributeStatement>
<Signature dsig="...">
<SignedInfo>
<CanonicalizationMethod Algorithm="...">
<SignatureMethod Algorithm="...">
<Reference uri="#...">
</SignedInfo>
<SignatureValue>
...
</SignatureValue>
<KeyInfo>
</KeyInfo>
</Signature>
</Assertion>
</Response>
此处的关键是Assertion
节点和导致问题的ID属性。有了这些知识,并使用说明中提到的解决方法#2,我就能解决这个问题:
...
Node sigNode = nodeList.item(0);
DOMValidateContext context = new DOMValidateContext(keySelector, sigNode);
context.setIdAttributeNS((Element) sigNode.getParentNode(), null, "ID");
XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
try {
XMLSignature signature = factory.unmarshalXMLSignature(context);
if (!signature.validate(context)) { // works now!
...
答案 1 :(得分:0)
我希望验证SOAP信封的Body部分,并且我使用了这个代码(我必须使用&#34; id&#34;小写,否则它没有成功):
DOMValidateContext valContext = new DOMValidateContext(publicKey, nl.item(0));
NodeList nlb = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body");
valContext.putNamespacePrefix("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "wsu");
Node body = nlb.item(0);
valContext.setIdAttributeNS((Element)body, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "id");
XML看起来像:
<soapenv:Body wsu:id="id-540d03b42e954c4ab5e3624b305b42fb4" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
感谢您的帮助!