本文整理匯總了Java中javax.xml.crypto.dsig.dom.DOMSignContext.putNamespacePrefix方法的典型用法代碼示例。如果您正苦於以下問題:Java DOMSignContext.putNamespacePrefix方法的具體用法?Java DOMSignContext.putNamespacePrefix怎麽用?Java DOMSignContext.putNamespacePrefix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.crypto.dsig.dom.DOMSignContext
的用法示例。
在下文中一共展示了DOMSignContext.putNamespacePrefix方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createAndConfigureSignContext
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
private DOMSignContext createAndConfigureSignContext(Node parent, KeySelector keySelector) {
DOMSignContext dsc = new DOMSignContext(keySelector, parent);
// set namespace prefix for "http://www.w3.org/2000/09/xmldsig#" according to best practice described in http://www.w3.org/TR/xmldsig-bestpractices/#signing-xml-without-namespaces
if (getConfiguration().getPrefixForXmlSignatureNamespace() != null
&& !getConfiguration().getPrefixForXmlSignatureNamespace().isEmpty()) {
dsc.putNamespacePrefix("http://www.w3.org/2000/09/xmldsig#", getConfiguration().getPrefixForXmlSignatureNamespace());
}
dsc.putNamespacePrefix("http://www.w3.org/2001/10/xml-exc-c14n#", "ec");
setCryptoContextProperties(dsc);
setUriDereferencerAndBaseUri(dsc);
return dsc;
}
示例2: signSignature
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
private Element signSignature(String id, Element env, KeyInfoFactory keyInfoFactory, X509Credential credential) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, MarshalException, XMLSignatureException {
if (endorsingToken == null) return env;
NodeList nl = env.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
if (e.hasAttributeNS(null, "Id")) {
e.setAttributeNS(WSSecurityConstants.WSU_NS, "Id", e.getAttribute("Id"));
e.setIdAttributeNS(WSSecurityConstants.WSU_NS, "Id", true);
}
}
env = SAMLUtil.loadElementFromString(XMLHelper.nodeToString(env));
DigestMethod digestMethod = xsf.newDigestMethod(DigestMethod.SHA1, null);
List<Transform> transforms = new ArrayList<Transform>(2);
transforms.add(xsf.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#",new ExcC14NParameterSpec(Collections.singletonList("xsd"))));
List<Reference> refs = new ArrayList<Reference>();
Reference r = xsf.newReference("#"+id, digestMethod, transforms, null, null);
refs.add(r);
CanonicalizationMethod canonicalizationMethod = xsf.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null);
SignatureMethod signatureMethod = xsf.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
SignedInfo signedInfo = xsf.newSignedInfo(canonicalizationMethod, signatureMethod, refs);
KeyInfo ki = generateKeyInfo(credential, keyInfoFactory, false);
XMLSignature signature = xsf.newXMLSignature(signedInfo, ki);
Node security = env.getElementsByTagNameNS(WSSecurityConstants.WSSE_NS, "Security").item(0);
DOMSignContext signContext = new DOMSignContext(credential.getPrivateKey(), security);
signContext.putNamespacePrefix(SAMLConstants.XMLSIG_NS, SAMLConstants.XMLSIG_PREFIX);
signContext.putNamespacePrefix(SAMLConstants.XMLENC_NS, SAMLConstants.XMLENC_PREFIX);
signature.sign(signContext);
return env;
}
示例3: sign
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
private void sign(KeyStore keyStore, KeyPair keyPair, String alias, Document document, List<EbMSDataSource> dataSources) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, KeyException, MarshalException, XMLSignatureException, KeyStoreException
{
//XMLSignatureFactory signFactory = XMLSignatureFactory.getInstance("DOM");
XMLSignatureFactory signFactory = XMLSignatureFactory.getInstance();
DigestMethod sha1DigestMethod = signFactory.newDigestMethod(DigestMethod.SHA1,null);
List<Transform> transforms = new ArrayList<Transform>();
transforms.add(signFactory.newTransform(Transform.ENVELOPED,(TransformParameterSpec)null));
Map<String,String> m = new HashMap<String,String>();
m.put("soap","http://schemas.xmlsoap.org/soap/envelope/");
transforms.add(signFactory.newTransform(Transform.XPATH,new XPathFilterParameterSpec("not(ancestor-or-self::node()[@soap:actor=\"urn:oasis:names:tc:ebxml-msg:service:nextMSH\"]|ancestor-or-self::node()[@soap:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"])",m)));
transforms.add(signFactory.newTransform(CanonicalizationMethod.INCLUSIVE,(TransformParameterSpec)null));
List<Reference> references = new ArrayList<Reference>();
references.add(signFactory.newReference("",sha1DigestMethod,transforms,null,null));
for (EbMSDataSource dataSource : dataSources)
references.add(signFactory.newReference("cid:" + dataSource.getContentId(),sha1DigestMethod,Collections.emptyList(),null,null,DigestUtils.sha(IOUtils.toByteArray(dataSource.getInputStream()))));
SignedInfo signedInfo = signFactory.newSignedInfo(signFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,(C14NMethodParameterSpec)null),signFactory.newSignatureMethod(SignatureMethod.RSA_SHA1,null),references);
List<XMLStructure> keyInfoElements = new ArrayList<XMLStructure>();
KeyInfoFactory keyInfoFactory = signFactory.getKeyInfoFactory();
keyInfoElements.add(keyInfoFactory.newKeyValue(keyPair.getPublic()));
Certificate[] certificates = keyStore.getCertificateChain(alias);
//keyInfoElements.add(keyInfoFactory.newX509Data(Arrays.asList(certificates)));
keyInfoElements.add(keyInfoFactory.newX509Data(Collections.singletonList(certificates[0])));
KeyInfo keyInfo = keyInfoFactory.newKeyInfo(keyInfoElements);
XMLSignature signature = signFactory.newXMLSignature(signedInfo,keyInfo);
Element soapHeader = getFirstChildElement(document.getDocumentElement());
DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),soapHeader);
signContext.putNamespacePrefix(XMLSignature.XMLNS,"ds");
signature.sign(signContext);
}
示例4: signDOM
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
public static void signDOM(Node node, PrivateKey privateKey, Certificate origCert) {
XMLSignatureFactory fac = initXMLSigFactory();
X509Certificate cert = (X509Certificate) origCert;
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List<Object> x509Content = new ArrayList<Object>();
x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element.
DOMSignContext dsc = new DOMSignContext(privateKey, node);
dsc.putNamespacePrefix("http://www.w3.org/2000/09/xmldsig#", "ns2");
// Create the XMLSignature, but don't sign it yet.
try {
SignedInfo si = initSignedInfo(fac);
XMLSignature signature = fac.newXMLSignature(si, ki);
// Marshal, generate, and sign the enveloped signature.
signature.sign(dsc);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例5: signDOM
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
private void signDOM(Node node, PrivateKey privateKey, Certificate origCert) {
XMLSignatureFactory fac = initXMLSigFactory();
X509Certificate cert = (X509Certificate) origCert;
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List<Object> x509Content = new ArrayList<Object>();
//x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element.
DOMSignContext dsc = new DOMSignContext(privateKey, node);
dsc.putNamespacePrefix(XML_DIGSIG_NS, "ns2");
// Create the XMLSignature, but don't sign it yet.
try {
SignedInfo si = initSignedInfo(fac);
XMLSignature signature = fac.newXMLSignature(si, ki);
// Marshal, generate, and sign the enveloped signature.
signature.sign(dsc);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例6: signElementByTag
import javax.xml.crypto.dsig.dom.DOMSignContext; //導入方法依賴的package包/類
public void signElementByTag(SOAPMessage soapMessage, String tag) throws Exception {
SOAPUtility.refreshSoap(soapMessage);
//Формируем новый документ из части сообщения
NodeList tagNodeList = soapMessage.getSOAPPart().getElementsByTagName(tag);
Document newXMLDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Node copyNode = newXMLDocument.importNode(tagNodeList.item(0), true);
newXMLDocument.appendChild(copyNode);
//Подписываемый элемент
NodeList newNodeList = newXMLDocument.getElementsByTagName(tag);
Element signedNode = (Element) newNodeList.item(0);
Provider xmlDSigProvider = new ru.CryptoPro.JCPxml.dsig.internal.dom.XMLDSigRI();
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", xmlDSigProvider);
List<Transform> transformList = new ArrayList<Transform>();
Transform transform = fac.newTransform(Transform.ENVELOPED, (XMLStructure) null);
Transform transformC14N = fac.newTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS, (XMLStructure) null);
transformList.add(transform);
transformList.add(transformC14N);
Reference ref = fac.newReference("", fac.newDigestMethod("http://www.w3.org/2001/04/xmldsig-more#gostr3411", null), transformList, null, null);
SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,
(C14NMethodParameterSpec) null),
fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#gostr34102001-gostr3411", null),
Collections.singletonList(ref));
KeyInfoFactory kif = fac.getKeyInfoFactory();
X509Data x509d = kif.newX509Data(Collections.singletonList(cert));
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(x509d));
javax.xml.crypto.dsig.XMLSignature sig = fac.newXMLSignature(si, ki);
DOMSignContext signContext = new DOMSignContext(privateKey, signedNode);
signContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS, "ds");
sig.sign(signContext);
//Заменяем исходный элемент на подписанный
Document doc = soapMessage.getSOAPPart().getEnvelope().getOwnerDocument();
Node signedTag = newXMLDocument.getFirstChild();
Node oldNode = tagNodeList.item(0);
Node parentNode = oldNode.getParentNode();
parentNode.removeChild(oldNode);
Node newNode = doc.importNode(signedTag, true);
parentNode.appendChild(newNode);
}