2010-06-07 22 views
8

PHP5'te Sabunla web servisini aramaya çalışıyorum, bunun için WS-Security 1.1 kullanmam gerekiyor. wp-güvenlik 1.1 php5 uygulamasının nasıl uygulanacağı

(java içinde ve .NET bu her otomatik olarak oluşturulur.)

PHP kolayca güvenlik başlıklarını oluşturmak için kullanılabilir herhangi çerçeveler var mı? Veya tüm başlığı kendim eklemek zorunda mıyım? WS-Security

Özellikler 1.1: PHP Classes günü http://oasis-open.org/committees/download.php/16790/wss-1.1-spec-os-SOAPMessageSecurity.pdf

cevap

12

Roger Veciana i Rovira bu (Ben sadece kod yeniden biçimlendirilmiş) gönderilen:,

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; 
    } 
} 
+0

Ben bu sürümü 1.0 içindir fark ettik ama Umarım sizi doğru yola koyar. – Artefacto

+0

Yanıtladığınız için şimdi bir sonraki özel durum var Yakalanmamış SoapFault istisnası: [HTTP] İleti işlenemiyor çünkü içerik türü 'text/xml; charset = utf-8 'beklenen tip' application/soap + xml değildi; karakter kümesi = UTF-8' . /home/projects/caheritage/site/providence/app/lib/core/WSSoapClient.php:80 herhangi bir fikir nasıl çözülür? –

+1

wsHttpBinding (SOAP 1.2) 'application/soap + xml' içerik türünü kullanır, basicHttpBinding (SOAP 1.1) 'text/xml' kullanır, böylece PHP ve WCF'nizin eşleştiğinden emin olun. yani - PHP'nin WCF'de temel veya ws bağlanırken doğru SOAP sürümünü kullanacak şekilde ayarlandığından emin olun – Xcalibur

İlgili konular