2

我在另一台服务器上有一个 SOAP 服务器 Web 服务和一个 SOAP 客户端来测试我的 Web 服务。

在 wsdl-mode 中,SOAP 服务器忽略 header 和 header 函数,从而忽略客户端身份验证细节。意味着客户端不进行身份验证。SOAP 服务器位于 https 域上。

SOAP 服务器和客户端在非 wsdl 模式下完美运行。

谁能告诉我我做错了什么?

这是我的 SOAP 客户端:

    $params = 'LHAFDS89';

    ini_set('soap.wsdl_cache_enabled',0);
    ini_set('soap.wsdl_cache',0);
    ini_set('soap.wsdl_cache_ttl',0);

    $client = new SoapClient('https://www.example.com/webservices/example.wsdl', array('trace'=>true));

    $authentication['key'] = "n/KLASDF9ASDF9832JDAFJ234=";

    $auth_key = $authentication['key'];
    $auth_mode = 't';

    $auth = new SOAPAuth($auth_key,$auth_mode);

    $header[] = new SoapHeader("urn:www.example.com",'authenticateClient',$auth,0);
    $client->__setSoapHeaders($header);

    try
    {
        $result = $client->soapFunction($params);
    }
    catch (SoapFault $e) 
    {
        echo "<script>alert('".$e->faultcode.'\n\n'.$e->faultstring."');</script>";
    }

    echo $result['message'];

class SOAPAuth 
{
    public $key;
    public $mode;

public function __construct($key, $mode) 
    {
      $this->key = $key;
      $this->mode = $mode;
    }
}

SOAP 服务器:

class ExampleClass {

    protected $user_is_valid;
    protected $mode;

    public function __construct()
    {
        $this->user_is_valid = false;
        $this->mode = '';
    }

    public function authenticateClient($header){
        if (isset($header->key) && isset($header->mode)){
            $authentication['key'] = $header->key;
            $this->mode = $header->mode;
            if($authentication['key']== 'n/KLASDF9ASDF9832JDAFJ234=')
            {
                $this->user_is_valid = true;
            }
        }
    } 

    public function soapFunction($params)
    {
        if($this->user_is_valid == true){

                $result['message'] = "User is valid."
                return $result; 

        } else {
            throw new SoapFault("Authorization:", "Failed!"); 
        }
    }
}

ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache',0);
ini_set('soap.wsdl_cache_ttl',0);

$example_class = new ExampleClass();
$server = new SoapServer("example.wsdl");

$server->setObject($example_class);
$server->handle();    

WSDL:

<?xml version="1.0" encoding="utf-8"?>

<definitions name="Example"
   targetNamespace="urn:Example"
   xmlns:typens="urn:Example"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
   xmlns="http://schemas.xmlsoap.org/wsdl/">

   <message name='header'>
     <part name='key' type='xsd:string'/>
     <part name='mode' type='xsd:string'/>
   </message>

   <message name='soapFunction_request'>
     <part name='params' type='xsd:string'/>
   </message>       
   <message name='soapFunction_response'>
     <part name='result' type='xsd:string[]'/>
   </message>

   <portType name='soapFunctionPortType'>
     <operation name='soapFunction'>
       <input message='tns:soapFunction_request'/>
       <output message='tns:soapFunction_response'/>
     </operation>
    </portType>

    <binding name='soapFunctionBinding' type='tns:soapFunctionPortType'>
    <soap:binding style='rpc'
         transport='http://schemas.xmlsoap.org/soap/http'/>
     <operation name='soapFunction'>
     <soap:operation soapAction='urn:soapFunction'/>
     <input>
        <soap:header message="header" part="key"/>
        <soap:header message="header" part="mode"/>
        <soap:body use='encoded' namespace='urn:soapFunction'
            encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
     </input>
     <output>
        <soap:body use='encoded' namespace='urn:soapFunction'
            encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
     </output>
     </operation>
      </binding>

      <service name='soapFunctionService'>
     <port name='soapFunctionServicePort' binding='soapFunctionBinding'>
      <soap:address location='https://www.example.com/webservices/soapServer.php'/>
     </port>
      </service>
</definitions> 

任何帮助,将不胜感激。

4

1 回答 1

1

我找到了解决我的问题的方法。这不完全是我的想法,但它确实有效。

我让 SOAP 客户端以 wsdl 模式连接到我的 SOAP 服务器,并让 SOAP 服务器以非 wsdl 模式响应。

由于某种原因,以这种方式工作时不会忽略标头,客户端会正确进行身份验证,并且会根据 wsdl 检查客户端的输入是否有效。

于 2013-01-17T09:07:09.223 回答