【发布时间】:2014-12-10 22:20:13
【问题描述】:
这个问题快把我逼疯了。我必须使用由 dotNet 端点应用程序验证的 Java 签署 SOAPMessage。供应商给了我们一个示例 dotNet 客户端应用程序作为示例,因此我受到了这个示例应用程序以及 Internet 上的几个链接的启发,如下所示:
http://www.java2s.com/Tutorial/Java/0410__Web-Services-SOA/SignSOAPmessage.htm
Java equivalent of C# XML signing method
正文(即签名内容)的结构是:
<Body>
<inbound>
<content>...text content...</content>
</inbound>
</Body>
到目前为止,仅当文本内容不包含任何换行符时,我才能够生成相同的签名(使用我的代码和供应商提供的示例代码)。一旦我的文本包含行分隔符,签名值就不再相同了。
经过几天的搜索,我认为问题与规范化有关。
在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);
...
生产:
<s:Body xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" Id="uuid"><inbound><content>ABC
DEF
HIJ
</content></inbound></s:Body>
而在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());
}
}
...
生产:
<s:Body xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" Id="uuid"><inbound><content>ABC
DEF
HIJ
</content></inbound></s:Body>
换行符仅是 LF。 (注意,原来的内容只包含 CR 作为换行符)。
如果需要,我可以提供更多源代码,但也许有人确切地知道这里发生了什么。任何帮助将不胜感激。请注意,我无法更改 dotNet 端点应用程序验证 XML 签名值的方式。
编辑 以下是消息正文的构建方式。我不明白为什么这不会产生与 Aaryn 的答案相同的结果:
public static void main(String[] args) throws Exception {
com.sun.org.apache.xml.internal.security.Init.init();
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement inboundMessage = soapBody.addChildElement("inbound");
SOAPElement payload = inboundMessage.addChildElement("content");
payload.addTextNode("ABC\rDEF\rGHI\r");
Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
byte outputBytes[] = c14n.canonicalizeSubtree(soapBody);
System.out.println(new String(outputBytes));
}
输出:
<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><inbound><content>ABC
DEF
GHI
</content></inbound></SOAP-ENV:Body>
编辑 2 我做了更多测试,尝试在运行时构建 XML,而不是解析输入字符串。这是我的最后一次试验:
public static void main(String[] args) throws Exception {
com.sun.org.apache.xml.internal.security.Init.init();
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element body = doc.createElement("Body");
Element inbound = doc.createElement("inbound");
Element content = doc.createElement("content");
content.appendChild(doc.createTextNode("ABC\rDEF\rGHI\r"));
doc.appendChild(body);
body.appendChild(inbound);
inbound.appendChild(content);
Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
byte outputBytes[] = c14n.canonicalizeSubtree(body);
System.out.println(new String(outputBytes));
}
而且输出仍然是
<Body><inbound><content>ABC
DEF
GHI
</content></inbound></Body>
我真的不明白。为什么 CR 被 &#xD; 代替而不是 LF 就像当文档是从输入字符串的解析中构建的一样?
【问题讨论】: