签名SOAP消息–生成封装的XML签名
生活随笔
收集整理的這篇文章主要介紹了
签名SOAP消息–生成封装的XML签名
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數字簽名是使數字內容真實可信的一種廣泛使用的機制。 通過為某些內容生成數字簽名,我們可以讓另一方能夠驗證該內容。 通過此驗證,它可以保證在我們簽名后不會更改。 通過這個示例,我將分享如何為SOAP信封生成簽名。 但是,這當然也適用于任何其他內容簽名。
在這里,我將簽名
- SOAP信封本身
- 一個附件
- 將簽名放在SOAP標頭中
通過將簽名放置在也由簽名簽名的SOAP標頭中,這成為封裝簽名的演示。
我正在使用Apache Santuario庫進行簽名。 以下是我使用的代碼段。 我已經在此處共享了完整的示例以供下載 。
public static void main(String unused[]) throws Exception {String keystoreType = "JKS";String keystoreFile = "src/main/resources/PushpalankaKeystore.jks";String keystorePass = "pushpalanka";String privateKeyAlias = "pushpalanka";String privateKeyPass = "pushpalanka";String certificateAlias = "pushpalanka";File signatureFile = new File("src/main/resources/signature.xml");Element element = null;String BaseURI = signatureFile.toURI().toURL().toString();//SOAP envelope to be signedFile attachmentFile = new File("src/main/resources/sample.xml");//get the private key used to sign, from the keystoreKeyStore ks = KeyStore.getInstance(keystoreType);FileInputStream fis = new FileInputStream(keystoreFile);ks.load(fis, keystorePass.toCharArray());PrivateKey privateKey =(PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());//create basic structure of signaturejavax.xml.parsers.DocumentBuilderFactory dbf =javax.xml.parsers.DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();Document doc = dBuilder.parse(attachmentFile);XMLSignature sig =new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);//optional, but betterelement = doc.getDocumentElement();element.normalize();element.getElementsByTagName("soap:Header").item(0).appendChild(sig.getElement());{Transforms transforms = new Transforms(doc);transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);//Sign the content of SOAP Envelopesig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);//Adding the attachment to be signedsig.addDocument("../resources/attachment.xml", transforms, Constants.ALGO_ID_DIGEST_SHA1);}//Signing procedure{X509Certificate cert =(X509Certificate) ks.getCertificate(certificateAlias);sig.addKeyInfo(cert);sig.addKeyInfo(cert.getPublicKey());sig.sign(privateKey);}//write signature to fileFileOutputStream f = new FileOutputStream(signatureFile);XMLUtils.outputDOMc14nWithComments(doc, f);f.close();}首先,它讀入用于簽名的私鑰。 要為您自己創建密鑰對, 這篇文章會有所幫助。 然后,它創建了簽名,并添加了SOAP消息和附件作為要簽名的文檔。 最后,它執行簽名并將簽名的文檔寫入文件。
簽名的SOAP消息如下所示。
<soap:Envelope xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:pj="http://www.pjxml.org/namespaces/messageHeader"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"><soap:Header><pj:MessageHeader pj:version="1.0" soap:mustUnderstand="1"><pj:From><pj:PartyId pj:type="ABCDE">FUN</pj:PartyId></pj:From><pj:To><pj:PartyId pj:type="ABCDE">PARTY</pj:PartyId></pj:To><pj:CPAId>uri:www.pjxml.org/socialService/Ping</pj:CPAId><pj:ConversationId>FUN PARTY FUN 59c64t0087fg3kfs000003n9</pj:ConversationId><pj:Service>uri:www.pjxml.org/socialService/</pj:Service><pj:Action>Ping</pj:Action><pj:MessageData><pj:MessageId>FUN 59c64t0087fg3kfs000003n9</pj:MessageId><pj:Timestamp>2013-10-22T17:12:20</pj:Timestamp></pj:MessageData></pj:MessageHeader><pj:Via pj:id="59c64t0087fg3ki6000003na" pj:syncReply="False" pj:version="1.0"soap:actor="http://schemas.xmlsoap.org/soap/actor/next" soap:mustUnderstand="1"><pj:Service>uri:www.pjxml.org/socialService/</pj:Service><pj:Action>Ping</pj:Action></pj:Via><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethodAlgorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"></ds:SignatureMethod><ds:Reference URI=""><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod><ds:DigestValue>9RXY9kp/Klx36gd4BULvST4qffI=</ds:DigestValue></ds:Reference><ds:Reference URI="../resources/attachment.xml"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod><ds:DigestValue>3JcccO8+0bCUUR3EJxGJKJ+Wrbc=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>d0hBQLIvZ4fwUZlrsDLDZojvwK2DVaznrvSoA/JTjnS7XZ5oMplN9 THX4xzZap3+WhXwI2xMr3GKO................x7u+PQz1UepcbKY3BsO8jB3dxWN6r+F4qTyWa+xwOFxqLj546WX35f8zT4GLdiJI5oiYeo1YPLFFqTrwg==</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate> MIIDjTCCAnWgAwIBAgIEeotzFjANBgkqhkiG9w0BAQsFADB3MQswCQYDVQQGEwJMSzEQMA4GA1UE...............qXfD/eY+XeIDyMQocRqTpcJIm8OneZ8vbMNQrxsRInxq+DsG+C92b</ds:X509Certificate></ds:X509Data><ds:KeyValue><ds:RSAKeyValue><ds:Modulus>k5y0amGgOQ2O/St0Kc2/xye80tX2fDEKs2YOlM/zCknL8VgK0CbAKVAwvJoycQL9mGRkPDmbitHe............StGofmsoKURzo8hofYEn41rGsq5wCuqJhhHYGDrPpFcuJiuI3SeXgcMtBnMwsIaKv2uHaPRbNX31WEuabuv6Q==</ds:Modulus><ds:Exponent>AQAB</ds:Exponent></ds:RSAKeyValue></ds:KeyValue></ds:KeyInfo></ds:Signature></soap:Header><soap:Body><pr:GetPriceResponse xmlns:pr="http://www.pushpalankajaya.com/prices"><pr:Price>1.90</pr:Price></pr:GetPriceResponse></soap:Body> </soap:Envelope>在下一篇文章中,讓我們看看如何驗證此簽名,以便我們可以保證簽名的文檔不會更改。
干杯!
翻譯自: https://www.javacodegeeks.com/2014/03/signing-soap-messages-generation-of-enveloped-xml-signatures.html
總結
以上是生活随笔為你收集整理的签名SOAP消息–生成封装的XML签名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器ddos攻击费用多少(服务器ddo
- 下一篇: 设计模式:模式或反模式,这就是问题