我正在尝试使用封装签名和 javax.xml.crypto.dsig.* 类对 xml 文件进行签名。结果,我得到了具有正确签名内容但没有定义命名空间的文件。如何添加 xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 命名空间和相应的 ds 前缀?我没有看到任何可以定义它的地方。
示例代码:
XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance("DOM");
(...)
XMLSignature signature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo);
// Marshal, generate, and sign the enveloped signature.
signature.sign(domSignContext);
给出示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns="http://different.namespace.com">
<someBody/>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>base64_digest</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>some_base64</SignatureValue>
<KeyInfo>
<X509Data>
<X509SubjectName>subject_data</X509SubjectName>
<X509Certificate>some_more_base64</X509Certificate>
</X509Data>
<KeyValue>
<RSAKeyValue>
<Modulus>another_base64</Modulus>
<Exponent>base64_as_well</Exponent>
</RSAKeyValue>
</KeyValue>
</KeyInfo>
</Signature>
</test>
但我想要:
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns="http://different.namespace.com" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<someBody/>
<ds:Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>base64_digest</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>some_base64</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509SubjectName>subject_data</ds:X509SubjectName>
<ds:X509Certificate>some_more_base64</ds:X509Certificate>
</ds:X509Data>
<ds:KeyValue>
<ds:RSAKeyValue>
<ds:Modulus>another_base64</ds:Modulus>
<ds:Exponent>base64_as_well</ds:Exponent>
</ds:RSAKeyValue>
</ds:KeyValue>
</ds:KeyInfo>
</ds:Signature>
</test>