我一直在寻找太久,无法弄清楚我做错了什么。
因此,我正在尝试为某些内容生成 Xades 签名。不幸的是,我总是遇到同样的错误:“HIERARCHY_REQUEST_ERR”。这是我的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<object>
<request id="f9e1294a-64b7-488b-b475-7511e317e399">(some arbitrary base64 encoded content)</request>
</object>
我正在尝试使用以下代码签署“请求”元素(显然......):
/*create a document*/
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element objectElement = doc.createElement("object");
doc.appendChild(objectElement);
Element requestElement = doc.createElement("request");
requestElement.appendChild(doc.createTextNode(decodedContent));
requestElement.setAttribute("id", UUID.randomUUID().toString());
objectElement.appendChild(requestElement);
/*Key provider, signing profile & signer itself*/
KeyingDataProvider kp = new CustomKeyingDataProvider(certificate, privateKey);
XadesSigningProfile p = new XadesTSigningProfile(kp);
XadesSigner signer = p.newSigner();
/*Signed data*/
DataObjectDesc flatFile = new DataObjectReference("#" + requestElement.getAttribute("id"))
.withTransform(new GenericAlgorithm("http://www.w3.org/2000/09/xmldsig#base64"))
.withDataObjectTimeStamp();
SignedDataObjects dataObjs = new SignedDataObjects(flatFile).withCommitmentType(AllDataObjsCommitmentTypeProperty.proofOfOrigin());
/*Actual signing*/
signer.sign(dataObjs, doc);
我得到这个错误作为回报(缩写为必要的):
class org.w3c.dom.DOMException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An
attempt was made to insert a node where it is not permitted.
at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
at xades4j.production.AppendAsLastChildStrategy.append(SignatureAppendingStrategies.java:55)
at xades4j.production.SignerBES.sign(SignerBES.java:210)
at xades4j.production.SignerBES.sign(SignerBES.java:122)
...
我在网上搜索,但我发现的唯一类似错误是这个:https ://code.google.com/p/xades4j/wiki/QeA (几乎在顶部)。我真的找不到他的问题的答案,但据我所知,我的 XML 文档带有一个根元素(就像他的第二个示例一样)。所以我真的不知道我做错了什么......
有没有人可以帮助我?提前致谢。