2015年2月11日

来源:互联网 发布:类似淘宝的交易平台 编辑:程序博客网 时间:2024/05/01 12:42

webservice 的security主要是指3方面的内容,即数据传输的完整性,机密性和身份认证,因为soap消息是基于xml的,所以soap 的security 也就是xml在传输过程中的完整性,机密性的问题。

xml本身对security的支持。

如何保证xml的完整性呢?是通过数字签名 来保证传输内容的完整性,什么是数字签名呢?个人理解,数字签名就是将要传输的内容按照一定的算法映射生成一串字符(也可能是其他内容比较小的形式),当接受者接到内容和签名的时候,用同样的算法将内容映射一遍,如果得到的字符串跟签名一致,就说明该内容没有被更改过。从而保证了内容的完整性。xml的数字签名可以对xml的元素进行签名,通过对xml元素的引用<Reference URI="">,将URI里面的内容替换为需要签名的内容。如果不添加就会对整个xml进行签名。


xml传输的机密性,保证传输的机密性,就是对传输的内容进行加密。个人理解,server端使用的是私钥,client 端使用的是公钥,公钥和私钥分别进行加密和解密。xml对于加密技术的支持包括可以加密整个xml文件,加密xml里面的某个元素,加密某个元素里面的内容等等。跟完整性用法不一样的是,EncryptedData将会替代被加密的元素,而不是引用xml元素。


关于身份验证,就是之前所说,在soap 的head里面可以加入用户名和密码的tag. jax-ws提供了一个handler接口,在这个接口里面可获得context和里面的用户名和密码,这样来确保webservice的身份验证。


在soap中应用xml的签字和加密技术。

<?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>

0 0
原创粉丝点击