【问题标题】:how to implement ws-security 1.1 in php5如何在php5中实现ws-security 1.1
【发布时间】:2011-02-28 14:18:54
【问题描述】:

我正在尝试使用 PHP5 中的 Soap 调用 Web 服务,为此,我需要使用 WS-Security 1.1。

(在 java 和 .NET 中,这都是自动生成的。)

是否有任何框架可用于在 PHP 中轻松生成安全标头?还是我必须自己添加整个标题?

WS-Security 1.1 规范:http://oasis-open.org/committees/download.php/16790/wss-1.1-spec-os-SOAPMessageSecurity.pdf

【问题讨论】:

    标签: php web-services zend-framework soap ws-security


    【解决方案1】:

    PHP Classes,Roger Veciana i Rovira 提交了这个(我刚刚重新格式化了代码):

    class WSSoapClient extends SoapClient {
    
        private $username;
        private $password;
        /*Generates de WSSecurity header*/
        private function wssecurity_header() {
    
            /* The timestamp. The computer must be on time or the server you are
             * connecting may reject the password digest for security.
             */
            $timestamp = gmdate('Y-m-d\TH:i:s\Z');
            /* A random word. The use of rand() may repeat the word if the server is
             * very loaded.
             */
            $nonce = mt_rand();
            /* This is the right way to create the password digest. Using the
             * password directly may work also, but it's not secure to transmit it
             * without encryption. And anyway, at least with axis+wss4j, the nonce
             * and timestamp are mandatory anyway.
             */
            $passdigest = base64_encode(
                    pack('H*',
                            sha1(
                                    pack('H*', $nonce) . pack('a*',$timestamp).
                                    pack('a*',$this->password))));
    
            $auth = '
    <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.'.
    'org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken>
        <wsse:Username>'.$this->username.'</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-'.
    'wss-username-token-profile-1.0#PasswordDigest">'.$passdigest.'</wsse:Password>
        <wsse:Nonce>'.base64_encode(pack('H*', $nonce)).'</wsse:Nonce>
        <wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-'.
    '200401-wss-wssecurity-utility-1.0.xsd">'.$timestamp.'</wsu:Created>
       </wsse:UsernameToken>
    </wsse:Security>
    ';
    
            /* XSD_ANYXML (or 147) is the code to add xml directly into a SoapVar.
             * Using other codes such as SOAP_ENC, it's really difficult to set the
             * correct namespace for the variables, so the axis server rejects the
             * xml.
             */
            $authvalues = new SoapVar($auth,XSD_ANYXML);
            $header = new SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-".
                "200401-wss-wssecurity-secext-1.0.xsd", "Security", $authvalues,
                    true);
    
            return $header;
        }
    
        /* It's necessary to call it if you want to set a different user and
         * password
         */
        public function __setUsernameToken($username, $password) {
            $this->username = $username;
            $this->password = $password;
        }
    
    
        /* Overwrites the original method adding the security header. As you can
         * see, if you want to add more headers, the method needs to be modifyed
         */
        public function __soapCall($function_name, $arguments, $options=null,
                $input_headers=null, $output_headers=null) {
    
            $result = parent::__soapCall($function_name, $arguments, $options,
                    $this->wssecurity_header());
    
            return $result;
        }
    }
    

    【讨论】:

    • 我注意到这是针对 1.0 版的,但希望它能让您走上正轨。
    • 感谢回复,现在我得到了下一个异常 Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' 不是预期的类型 'application/soap+xml;字符集=utf-8'。在 /home/projects/caheritage/site/providence/app/lib/core/WSSoapClient.php:80 知道如何解决吗?
    • wsHttpBinding (SOAP 1.2) 使用 'application/soap+xml' 内容类型,basicHttpBinding (SOAP 1.1) 使用 'text/xml' 所以确保你的 PHP 和 WCF 匹配。 ie - 在 WCF 中调用基本或 ws 绑定时,确保 PHP 设置为使用正确的 SOAP 版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多