33

パスワードで保護され、URL が https である Web サービスに接続しようとしています。スクリプトがリクエストを行う前に認証する方法がわかりません。サービスを定義するとすぐにリクエストが行われるようです。たとえば、次のように入力すると:

$client = new SoapClient("https://example.com/WSDL/nameofservice",
       array('trace' => 1,)
);

ブラウザでサイトにアクセスすると、次のようになります。

Fatal error: Uncaught SoapFault exception: 
[WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from
'https://example.com/WSDL/nameofservice' in /path/to/my/script/myscript.php:2 
Stack trace: #0 /path/to/my/script/myscript.php(2): 
SoapClient->SoapClient('https://example...', Array) #1 {main} thrown in 
/path/to/my/script/myscript.php on line 2

サービスを SOAP サーバーとして定義しようとすると、次のようになります。

$server= new SoapServer("https://example.com/WSDL/nameofservice");

私は得る:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>WSDL</faultcode>
<faultstring>
SOAP-ERROR: Parsing WSDL: 
Couldn't load from 'https://example.com/WSDL/nameofservice'
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

サーバーが何を返すかを確認するために未加工のリクエスト エンベロープを送信することはまだ試していませんが、回避策になる可能性があります。しかし、php組み込みクラスを使用して設定する方法を誰かが教えてくれることを望んでいました。配列に「userName」と「password」を追加してみましたがだめでした。問題は、リクエストを拒否しているかどうかは言うまでもなく、リモート サイトに到達しているかどうかさえまったくわからないことです。

4

7 に答える 7

36

SoapHeader を拡張するだけで、Wsse 準拠の認証を作成できます。

class WsseAuthHeader extends SoapHeader {

private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

function __construct($user, $pass, $ns = null) {
    if ($ns) {
        $this->wss_ns = $ns;
    }

    $auth = new stdClass();
    $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns); 
    $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

    $username_token = new stdClass();
    $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns); 

    $security_sv = new SoapVar(
        new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
        SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
    parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}



$wsse_header = new WsseAuthHeader($username, $password);
$x = new SoapClient('{...}', array("trace" => 1, "exception" => 0));
$x->__setSoapHeaders(array($wsse_header));

ノンスとタイムスタンプで ws-security を使用する必要がある場合、Peter は更新バージョンをhttp://php.net/manual/en/soapclient.soapclient.php#114976に投稿しました。彼:

class WsseAuthHeader extends SoapHeader
{
    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';

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

        $auth           = new stdClass();
        $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Nonce    = new SoapVar($passdigest, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Created  = new SoapVar($created, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wsu_ns);

        $username_token                = new stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
            new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
            SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }
}

回答に記載されている詳細と比較してください https://stackoverflow.com/a/18575154/367456

于 2011-07-13T11:10:04.147 に答える
29

問題は、WSDL ドキュメントが何らかの形で保護されていること (基本認証 - でダイジェスト認証がサポートされているとは思わないSoapClientため、この場合はうまくいかないことです) であるSoapClientため、サービスを読み取ったり解析したりできないことです。説明。

まず、ブラウザで WSDL の場所を開いて、認証ダイアログが表示されるかどうかを確認してください。認証ダイアログが表示される場合は、SoapClientが WSDL ドキュメントの取得時に必要なログイン資格情報を使用していることを確認する必要があります。問題は、およびオプション (および証明書認証を使用する場合のオプション) でSoapClient指定された資格情報のみが、サービスの呼び出し時にクライアントを作成するときに送信され、WSDL を取得するときに送信されないことです (こちらを参照)。この問題を解決するには、次の 2 つの方法があります。loginpasswordlocal_cert

  1. SoapClientコンストラクター呼び出しで WSDL URL にログイン資格情報を追加します。

    $client = new SoapClient(
        'https://' . urlencode($login) . ':' . urlencode($password) . '@example.com/WSDL/nameofservice',
        array(
            'login' => $login,
            'password' => $password
        )
    );
    

    これは最も単純な解決策であるはずですが、PHPバグ #27777には、これも機能しないと書かれています (試したことはありません)。

  2. HTTP ストリーム ラッパーを使用して手動で WSDL を取得するか、ブラウザまたはたとえばext/curl経由で手動で WSDL を取得し、ディスクに保存して、ローカル WSDL への参照を使用して をインスタンス化します。wgetSoapClient

    このソリューションは、変更を検出して新しいバージョンをディスクに保存する必要があるため、WSDL ドキュメントが変更された場合に問題になる可能性があります。

認証ダイアログが表示されず、ブラウザで WSDL を読み取ることができる場合は、さらに詳細を入力して、他のエラー/問題の可能性を確認する必要があります。

この問題は、サービス自体SoapClientへの呼び出しを発行する前に、サービスの説明ドキュメントを既に読んでいるため、サービス自体とは明らかに関係ありません。

編集:

WSDL ファイルをローカルに置くことが最初のステップです。これによりSoapClient、 はサービスと通信する方法を知ることができます。WSDL がサービスの場所から直接提供されているか、別のサーバーから提供されているか、ローカル ファイルから読み取られているかは問題ではありません。サービス URL は WSDL 内でコード化されているSoapClientため、サービス エンドポイントを探す場所を常に認識しています。

2 つ目の問題はSoapClientWS-SecuritySoapClient仕様をネイティブでサポートしていないことです。つまり、特定のヘッダーを処理するには拡張する必要があります。必要な動作を追加するための拡張ポイントはSoapClient::__doRequest()、サービス エンドポイントに送信する前に XML ペイロードを前処理することです。しかし、WS-Security ソリューションを自分で実装するには、特定の WS-Security 仕様に関する十分な知識が必要になると思います。SoapClient::__setSoapHeaders()おそらく、適切なsを使用して WS-Security ヘッダーを作成し、XML 要求にパックすることもできますがSoapHeader、これがうまくいくとは思えず、カスタムSoapClient拡張を唯一の可能性として残しています。

簡単なSoapClient拡張は次のようになります

class My_SoapClient extends SoapClient
{
    protected function __doRequest($request, $location, $action, $version) 
    {
        /*
         * $request is a XML string representation of the SOAP request
         * that can e.g. be loaded into a DomDocument to make it modifiable.
         */
        $domRequest = new DOMDocument();
        $domRequest->loadXML($request);

        // modify XML using the DOM API, e.g. get the <s:Header>-tag 
        // and add your custom headers
        $xp = new DOMXPath($domRequest);
        $xp->registerNamespace('s', 'http://www.w3.org/2003/05/soap-envelope');
        // fails if no <s:Header> is found - error checking needed
        $header = $xp->query('/s:Envelope/s:Header')->item(0);

        // now add your custom header
        $usernameToken = $domRequest->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:UsernameToken');
        $username = $domRequest->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:Username', 'userid');
        $password = $domRequest->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:Password', 'password');
        $usernameToken->appendChild($username);
        $usernameToken->appendChild($password);
        $header->appendChild($usernameToken);

        $request = $domRequest->saveXML();
        return parent::__doRequest($request, $location, $action, $version);
    }
}

基本的な WS-Security 認証の場合、SOAP ヘッダーに次を追加する必要があります。

<wsse:UsernameToken>
    <wsse:Username>userid</wsse:Username>
    <wsse:Password>password</wsse:Password>                                 
</wsse:UsernameToken>

しかし、上で述べたように、これを機能させるには、WS-Security 仕様と指定されたサービス アーキテクチャに関するより多くの知識が必要だと思います。

WS-* 仕様範囲全体に対応するエンタープライズ レベルのソリューションが必要で、PHP モジュールをインストールできる場合は、WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) を参照してください。

于 2009-06-05T10:43:32.320 に答える
19

パスワード ダイジェスト セキュリティの場合、以下を使用できます。

   /**
    * This function implements a WS-Security digest authentification for PHP.
    *
    * @access private
    * @param string $user
    * @param string $password
    * @return SoapHeader
    */
   function soapClientWSSecurityHeader($user, $password)
   {
      // Creating date using yyyy-mm-ddThh:mm:ssZ format
      $tm_created = gmdate('Y-m-d\TH:i:s\Z');
      $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180); //only necessary if using the timestamp element

      // Generating and encoding a random number
      $simple_nonce = mt_rand();
      $encoded_nonce = base64_encode($simple_nonce);

      // Compiling WSS string
      $passdigest = base64_encode(sha1($simple_nonce . $tm_created . $password, true));

      // Initializing namespaces
      $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
      $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
      $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest';
      $encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';

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

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

      //the timestamp element is not required by all servers
      $timestamp = $security->addChild('wsu:Timestamp', null, $ns_wsu);
      $timestamp->addAttribute('wsu:Id', 'Timestamp-28');
      $timestamp->addChild('wsu:Created', $tm_created, $ns_wsu);
      $timestamp->addChild('wsu:Expires', $tm_expires, $ns_wsu);

      $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
      $usernameToken->addChild('wsse:Username', $user, $ns_wsse);
      $usernameToken->addChild('wsse:Password', $passdigest, $ns_wsse)->addAttribute('Type', $password_type);
      $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse)->addAttribute('EncodingType', $encoding_type);
      $usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);

      // Recovering XML value from that object
      $root->registerXPathNamespace('wsse', $ns_wsse);
      $full = $root->xpath('/root/wsse:Security');
      $auth = $full[0]->asXML();

      return new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true);
   }

PHP SoapClient で使用するには、次の方法を使用します。

$client = new SoapClient('http://endpoint');
$client->__setSoapHeaders(soapClientWSSecurityHeader('myUser', 'myPassword'));
// $client->myService(array('param' => 'value', ...);
于 2013-09-02T14:08:05.963 に答える
7

既存のsoapclientライブラリを拡張するよりも簡単な解決策があります。

ステップ1:2つのクラスを作成して、WSSEヘッダーの構造を作成します

class clsWSSEAuth {
    private $Username;
    private $Password;
    function __construct($username, $password) {
        $this->Username=$username;
        $this->Password=$password;
    }
}

class clsWSSEToken {
    private $UsernameToken;
    function __construct ($innerVal){
        $this->UsernameToken = $innerVal;
    }
}

ステップ2:ユーザー名とパスワードのSoap変数を作成する

$username = 1111;
$password = 1111;

//Check with your provider which security name-space they are using.
$strWSSENS = "http://schemas.xmlsoap.org/ws/2002/07/secext";

$objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
$objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);

ステップ3:認証クラスのオブジェクトを作成し、soapvarを渡します

$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);

ステップ4:AuthクラスのオブジェクトからSoapVarを作成します

$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);

ステップ5:トークンクラスのオブジェクトを作成する

$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);

ステップ6:TokenクラスのオブジェクトからSoapVarを作成します

$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);

ステップ7:「セキュリティ」ノードのSoapVarを作成する

$objSoapVarHeaderVal=new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);

ステップ8:セキュリティsoapvarからヘッダーオブジェクトを作成する

$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal,true, 'http://abce.com');

//Third parameter here makes 'mustUnderstand=1
//Forth parameter generates 'actor="http://abce.com"'

ステップ9:SoapClientのオブジェクトを作成します

$objClient = new SoapClient($WSDL, $arrOptions);

ステップ10:soapclientオブジェクトのヘッダーを設定する

$objClient->__setSoapHeaders(array($objSoapVarWSSEHeader));

ステップ11:メソッドの最後の呼び出し

$objResponse = $objClient->__soapCall($strMethod, $requestPayloadString);
于 2010-05-07T21:02:38.163 に答える
1

Alain Tiemblo の優れたソリューションを採用しましたが、ダイジェストではなくパスワードを使用しています。

    /**
    * This function implements a WS-Security authentication for PHP.
    *
    * @access private
    * @param string $user
    * @param string $password
    * @return SoapHeader
    */
    function soapClientWSSecurityHeader($user, $password)
   {
      // Creating date using yyyy-mm-ddThh:mm:ssZ format
      $tm_created = gmdate('Y-m-d\TH:i:s\Z');
      $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180); //only necessary if using the timestamp element

      // Generating and encoding a random number
      $simple_nonce = mt_rand();
      $encoded_nonce = base64_encode($simple_nonce);

      // Compiling WSS string
      $passdigest = base64_encode(sha1($simple_nonce . $tm_created . $password, true));

      // Initializing namespaces
      $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
      $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
      $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
      $encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';

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

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

      //the timestamp element is not required by all servers
      $timestamp = $security->addChild('wsu:Timestamp', null, $ns_wsu);
      $timestamp->addAttribute('wsu:Id', 'Timestamp-28');
      $timestamp->addChild('wsu:Created', $tm_created, $ns_wsu);
      $timestamp->addChild('wsu:Expires', $tm_expires, $ns_wsu);

      $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
      $usernameToken->addChild('wsse:Username', $user, $ns_wsse);
      $usernameToken->addChild('wsse:Password', $password, $ns_wsse)->addAttribute('Type', $password_type);
      $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse)->addAttribute('EncodingType', $encoding_type);
      $usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);

      // Recovering XML value from that object
      $root->registerXPathNamespace('wsse', $ns_wsse);
      $full = $root->xpath('/root/wsse:Security');
      $auth = $full[0]->asXML();

      return new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true);
   }

それを呼び出すには、使用します

$client = new SoapClient('YOUR ENDPOINT');
$userid = "userid";
$password = "password"; 
$client->__setSoapHeaders(soapClientWSSecurityHeader($userid,$password));
于 2016-03-05T23:50:55.063 に答える
0
$client = new SoapClient("some.wsdl", array('login'    => "some_name",
                                            'password' => "some_password"));

PHPのドキュメントから

于 2009-06-04T23:47:31.540 に答える