在分别介绍了XML Signature和XML Encryption后,我们了解了如何用XML的形式来保证消息的完整性(Integrity)和机密性(Confidentiality)。如何将其运用到Web Service中从而保证Web Service的安全性,这就是WS-Security规范所描述的内容。我们知道Web Service的采用SOAP作为消息封装协议, 因此WS-Security规范主要描述了如何将XML Security(XML Signature和XML Encryption)与已有的安全技术(Kerberos,X.509,SAML)结合, 并把它们绑定到SOAP中.
NoteSOAP描述了Web Service消息的打包形式,而非消息的传输协议,Web Service的传输协议可以是http,tcp甚至smtp。
(注意这幅图只是一张示意图,比如ds:Signature也可以对Soap Head中的元素做签名)
从之前对XML Signature和XML Encryption介绍中,我们已经了解了签名和加密的具体过程,以及在整个过程中产生的各个元素的含义。下面就从一个具体的运用了WS-Security规范的Soap Envelop中来看看如何在WS-Security中使用XML Signature和XML Encryption。
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc"
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<SOAP-ENV:Header>
<wsse:Security
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/secext">
<wsse:UsernameToken wsu:Id="UserToken">
<wsse:Username>HotelService</wsse:Username>
<wsse11:Salt>sdfer..</wsse11:Salt>
<wsse11:Iteration>1000</wsse11:Iteration>
</wsse:UsernameToken>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod
Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"/>
<ds:Reference URI="#DiscountedBookingForPartnersResponse">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>JwFsd3eQc0iXlJm5PkLh7...</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>BSxlJbSiFdm5Plhk...</ds:SignatureValue>
<ds:KeyInfo>
<wsse:SecurityTokenReference>
<wsse:Reference URI="#UserToken"/>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
<xenc:ReferenceList>
<xenc:DataReference URI="#DiscountResponse"/>
</xenc:ReferenceList>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body wsu:Id="DiscountedBookingForPartnersResponse">
<s:GetSpecialDiscountedBookingForPartnersResponse
xmlns:s=?http://www.MyHotel.com/partnerservice?>
<xenc:EncryptedData
wsu:Id="DiscountResponse"
type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#aes256_cbc "/>
<ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
<wsse:SecurityTokenReference>
<wsse:Reference URI="#UserToken"/>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
<CipherData>
<CipherValue>XDsFaDWsHdhrHdhcW0x...</CipherValue>
</CipherData>
</xenc:EncryptedData>
</s:GetSpecialDiscountedBookingForPartnersResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>