上一节介绍了 怎样实现基本认证 (Basic Authentication ,以下简称 basic 方式 ) ,望文生义,也就是最简单的用户验证方式,本节稍微深入一些,介绍用户名令牌认证 (Usernametoken Authentication ,以下简称 usernametoken 方式 ) 。
Usernametoken 方式与 basic 方式不同的地方,在于后者会把用户名和密码以摘要 (digest) 的形式,置于 HTTP 信息头,而前者则把用户名以明文的形式、密码以明文或者摘要的形式,嵌入到一段 XML 文本中,再置于 SOAP 消息头当中。
如果使用 soapUI 调试客户端程序的话,会发现以下是 basic 方式发出的完整的 SOAP 消息:
POST https://test2.r-secure.com/Services/ECHO HTTP/0.9
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
User-Agent: Jakarta Commons-HttpClient/3.1
Content-Length: 292
Authorization: Basic VkYtSEstbVNNST0OdlR42EMZaD1BMyE=
Host: test2.r-secure.com
Cookie: $Version=0; MSP2LB=test2.test2f02; $Path=/
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.rsecure.com/ECHO">
<soapenv:Header/>
<soapenv:Body>
<echo:echo>
<echo:EchoMessage>hello</echo:EchoMessage>
</echo:echo>
</soapenv:Body>
</soapenv:Envelope>
以下是 usernametoken 方式发出的完整的 SOAP 消息:
POST https://test.r-secure.com/4.0/services/SecureEcho HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
User-Agent: Jakarta Commons-HttpClient/3.1
Host: test.r-secure.com
Content-Length: xxx
<soapenv:Envelope xmlns:echo="http://echo.ws.rsecure.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-32870670" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>roy</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">liang</wsse:Password>
<wsse:Nonce>LX4gh+njbEtCNAtkWkXDYA==</wsse:Nonce>
<wsu:Created>2010-08-11T06:02:25.874Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
<echo:customerId>G06164</echo:customerId>
</soapenv:Header>
<soapenv:Body>
<echo:sendEcho>
<echo:message>hello</echo:message>
</echo:sendEcho>
</soapenv:Body>
</soapenv:Envelope>
其中,加粗部分表示两者的主要区别,红字部分表示各自必不可少的元素。
由此可以看出, usernametoken 方式的特点,是在 SOAP 的 header 中加入一个 Security 标签,把 usernametoken 信息放在这个 Security 标签里。至于 header 里的另外一个 customerId 标签,是该应用自身的额外要求。
gSOAP 实现 basic