douhua9726 2015-12-07 20:53
浏览 129

使用具有安全模式“TransportWithMessageCredential”的SoapClient使用PHP创建SOAP标头

How can I construct a SOAP header using the "TransportWithMessageCredential" mode in PHP using the SoapClient. I'm using the SoapClient as this seems to be the best solution. The following is from the given documentation:

The Webservice uses the security mode "TransportWithMessageCredential“. For secure transmission an SSL certificate is being used. Furthermore, for the security of the message exchange, a combination of username and password is required. The username and password are transmitted in the SOAP-Header.

Example:

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

Where "MyUserName" and "MyPassword!" ofc. is interchanged with the actual login information.

wsdl's are available for every functionality offered.

  • 写回答

1条回答 默认 最新

  • dongmaonao0505 2015-12-21 20:10
    关注

    This class returns a header with the above defined format:)

    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);
    
        }
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog