这个问题让我抓狂.我必须使用由dotNet端点应用程序验证的Java签署SOAPMessage.供应商给了我们一个示例dotNet客户端应用程序作为示例,所以我受到了这个示例应用程序的启发,以及互联网上的几个链接,如下所示:
http://www.java2s.com/Tutorial/Java/0410__Web-Services-SOA/SignSOAPmessage.htm
正文的结构(签名内容)是:
<Body>
<inbound>
<content>...text content...</content>
</inbound>
</Body>
Run Code Online (Sandbox Code Playgroud)
到目前为止,只有当文本内容不包含任何换行符时,我才能够生成相同的签名(使用我的代码和供应商提供的示例).只要我的文本包含行分隔符,签名值就不再相同.
经过几天的搜索,我认为这个问题与规范化有关.
在Java中,以下代码:
...
Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
Element body = getNextSiblingElement(header);
byte outputBytes[] = c14n.canonicalizeSubtree(body);
FileUtils.writeByteArrayToFile(new File("C:\\tmp\\Canonicalized_java.xml"),outputBytes);
...
Run Code Online (Sandbox Code Playgroud)
生产:
<s:Body xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" Id="uuid"><inbound><content>ABC
DEF
HIJ
</content></inbound></s:Body>
Run Code Online (Sandbox Code Playgroud)
在dotNet(c#)中,代码如下:
...
using (MemoryStream nodeStream = new MemoryStream())
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.NewLineHandling = NewLineHandling.None;
using (XmlWriter xw = XmlWriter.Create(nodeStream, ws))
{
soapRequest.GetElementsByTagName("Body")[0].WriteTo(xw);
xw.Flush();
}
nodeStream.Position = 0;
XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform();
transform.LoadInput(nodeStream);
using (MemoryStream outputStream = (MemoryStream)transform.GetOutput(typeof(Stream)))
{
File.WriteAllBytes("C:\\tmp\\Canonicalized_dotNet.xml", outputStream.ToArray());
}
}
... …Run Code Online (Sandbox Code Playgroud)