簡體   English   中英

使用安全模式為“ TransportWithMessageCredential”的SoapClient使用PHP創建SOAP標頭

[英]Creating a SOAP header with PHP using SoapClient with security mode "TransportWithMessageCredential“

如何使用SoapClient在PHP中使用“ TransportWithMessageCredential”模式構造SOAP標頭。 我正在使用SoapClient,因為這似乎是最好的解決方案。 以下是給定文檔中的內容:

Web服務使用安全模式“ TransportWithMessageCredential”。為了安全傳輸,正在使用SSL證書。此外,為了消息交換的安全性,需要用戶名和密碼的組合。用戶名和密碼在SOAP-Header中傳輸。

例:

<soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasisopen.
    org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss wssecurityutility-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-37">
            <wsse:Username>MyUsername</wsse:Username>
            <wsse:Password Type="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword!</wsse:Password>
        <wsse:Nonce EncodingType="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">vAvnhyzl+yP8Yb8ZVdKnMw==</wsse:Nonce>
        <wsu:Created>2014-03-17T13:08:02.795Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>  

其中“ MyUserName”和“ MyPassword!” 辦公室 與實際的登錄信息互換。

wsdl可用於提供的所有功能。

此類返回具有上述定義格式的標頭:)

class WsseAuthHeader extends SoapHeader
{
    private $wssNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    private $wsuNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
    private $passType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    private $nonceType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
    private $username = 'Username';
    private $password = 'Password';


    function __construct()
    {
        $created = gmdate('Y-m-d\TH:i:s\Z');
        $nonce = mt_rand();
        $encodedNonce = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*', $created) . pack('a*', $this->password))));

        // Creating WSS identification header using SimpleXML
        $root = new SimpleXMLElement('<root/>');

        $security = $root->addChild('wsse:Security', null, $this->wssNs);

        $usernameToken = $security->addChild('wsse:UsernameToken', null, $this->wssNs);
        $usernameToken->addChild('wsse:Username', $this->username, $this->wssNs);
        $passNode = $usernameToken->addChild('wsse:Password', htmlspecialchars($this->password, ENT_XML1, 'UTF-8'), $this->wssNs);
        $passNode->addAttribute('Type', $this->passType);

        $nonceNode = $usernameToken->addChild('wsse:Nonce', $encodedNonce, $this->wssNs);
        $nonceNode->addAttribute('EncodingType', $this->nonceType);
        $usernameToken->addChild('wsu:Created', $created, $this->wsuNs);
        // Recovering XML value from that object
        $root->registerXPathNamespace('wsse', $this->wssNs);
        $full = $root->xpath('/root/wsse:Security');
        $auth = $full[0]->asXML();

        parent::SoapHeader($this->wssNs, 'Security', new SoapVar($auth, XSD_ANYXML), true);

    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM