[英]Modify namespaced XML using JavaScript with jQuery
我有一個命名空間的XML文檔,必須使用特殊的瀏覽器插件簽名。
這是和平代碼,即簽名文件:
var oCertificate = GetCertificateBySubjectName(certificateName);
var token = oCertificate.Export(CADESCOM_ENCODE_BASE64);
var element, xmlDoc;
xmlDoc = $.parseXML(doc.toString());
element = $(xmlDoc).find("o\\:BinarySecurityToken");
element.text(token);
var xmlString = undefined;
if (window.ActiveXObject) {
xmlString = xmlDoc[0];
}
if (xmlString === undefined) {
var oSerializer = new XMLSerializer();
xmlString = (new XMLSerializer()).serializeToString(xmlDoc);
}
var doc = SignCreate(oCertificate, xmlString);
doc是包含XML的字符串。
這是XML的安全性,必須簽名:
<s:Header>
<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:actor="http://smev.gosuslugi.ru/actors/smev">
<o:BinarySecurityToken u:Id="uuid-ee82d445-758b-42cb-996c-666b74b60022-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"/>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gostr34102001-gostr3411" />
<Reference URI="#_1">
<Transforms>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gostr3411" />
<DigestValue/>
</Reference>
</SignedInfo>
<SignatureValue/>
<KeyInfo>
<o:SecurityTokenReference>
<o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#uuid-ee82d445-758b-42cb-996c-666b74b60022-2" />
</o:SecurityTokenReference>
</KeyInfo>
</Signature>
</o:Security>
</s:Header>
文檔的簽名是這樣的。 使用xmlDoc = $.parseXML(message.toString()); element = $(xmlDoc).find("o\\\\:BinarySecurityToken"); element.text(token);
xmlDoc = $.parseXML(message.toString()); element = $(xmlDoc).find("o\\\\:BinarySecurityToken"); element.text(token);
我將令牌從證書放入<o:BinarySecurityToken>
然后將其轉換回字符串並發送給簽名。
在此步驟中,我得到了:
<o:BinarySecurityToken u:Id="uuid-ee82d445-758b-42cb-996c-666b74b60022-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">!!TOKEN!!</o:BinarySecurityToken>
接着
<o:BinarySecurityToken u:Id="uuid-ee82d445-758b-42cb-996c-666b74b60022-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">!!!TOKEN!!!</o:BinarySecurityToken>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gostr34102001-gostr3411"/>
<Reference URI="#_1">
<Transforms>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gostr3411"/>
<DigestValue>!!!SIGNATURE DIGEST VALUE!!!</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>!!!SIGNATURE!!!</SignatureValue>
一切在FireFox和(!)IE中都可以正常運行,但在Google Chrome中卻無法運行。 在將令牌置為空白的Chrome代碼中,其他所有方法均無效。
所以,我的問題是:我應該怎么解決這個問題? 我嘗試使用https://github.com/rfk/jquery-xmlns賦予jQuery一些使用命名空間XML的功能,但是該庫未在我的代碼中運行。
提前致謝。
PS我使用jQuery 1.10.2
jQuery不支持名稱空間,僅支持節點名稱中的冒號。 我無法使jquery-xmlns插件與當前的jQuery版本一起使用。
新的Document.querySelector()
和Document.querySelectorAll()
方法也不支持名稱空間。
但是Document.evaluate()
和Document
本身可以。 他們允許使用XPath。 除IE外,所有現代瀏覽器均支持Document.evaluate()
。 對於IE,可以使用JavaScript庫將方法添加到文檔對象。
var dom = (new DOMParser()).parseFromString(xml, 'application/xml');
var resolver = {
namespaces : {
'o' : 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
},
lookupNamespaceURI : function(prefix) {
if (prefix == '') {
return null;
}
return this.namespaces[prefix] || null;
}
};
var node = dom.evaluate(
'//o:BinarySecurityToken',
dom,
resolver,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
node.appendChild(dom.createTextNode('TOKEN_TEXT'));
document.querySelector('#output').textContent = (new XMLSerializer()).serializeToString(dom);
演示: http : //jsfiddle.net/mec2qxLa/2/
要使其與IE配合使用,您需要加載xpath.js。 它將評估方法附加到document
對象。 對於新的Document
實例,您可以從那里獲取。 它不發布XPathResult
對象,但是定義了提供它的xpath
對象。
var dom = (new DOMParser()).parseFromString(xml, 'application/xml');
if (!dom.evaluate && document.evaluate) {
dom.evaluate = document.evaluate;
if (typeof XPathResult == 'undefined') {
XPathResult = xpath.XPathResult;
}
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.