UsernameToken認証とHTTPSを使用したWSF / PHPに基づくWebサービスの作成

原則としてWebサービスを処理し、WSおよびWS-Security標準に従ってアクセスできるPHPでサービス (サービスクライアントではない)を作成する可能性について学習するタスクを与えられました。 また、.NETクライアントとの互換性が必要でした(私の場合、WCFを使用しないで十分でした)。



短いグーグルの後、標準のPHP SOAPライブラリもZend Frameworkも、WSF / PHP以外のファイルもファイルなしのWS-Securityをサポートしていないことが明らかになりました。プレーンテキストパスワード(理想的には、署名、暗号化、証明書など)が必要です。



このソリューションは、WSDL生成など、WSF / PHPフレームワークのすべての機能を説明するものではありません。これはすべてドキュメントで説明されており、 ドキュメントの観点から重要なソリューションを必要としないためです。 ライブラリのバージョン2.1.0で作業します。





準備する


まず、必要なすべてのコンポーネントがインストールされていることを確認してください。 私のサービスはUbuntu 11.04を有効にし、クライアントアプリケーションはC#で作成されました(以下のリストで、コードと構成を示します)。 WSF / PHPと必要なサービスを使用するには:



すべてのコンポーネントのインストールと構成は、インターネット上の多くのリソースで説明されています。インストールと構成は、ドキュメントで説明されています(概要のリンクを参照)。 PHPモジュールを設定するとき、 wsf.homeオプションはライブラリがインストールされているパスを示していることに注意してください(私の場合は/opt/wso2/wsf_c



)。



サービス


私たちのサービスは、与えられた引数を二乗して返す唯一の操作です。 簡単に言えば、コードの内容: findSquare



関数。実際にタスクを実行します。 操作名を関数名にバインドし、セキュリティポリシーを宣言してトークンを作成します(この例では、ユーザー名とパスワードはハードコーディングされていpasswordCallback



が、WSSecurityTokenコンストラクターでAPIを参照)( APIを参照)。データベースのパスワード、およびさらなる検証のためにそれを返す); すべてのパラメーターと処理呼び出しでWSServiceのインスタンスを作成します。



サービスがindex.php



ファイルに保存されていると仮定します



 <?php function findSquare($integer) { $result = pow($integer, 2); return array("result"=>$result); } $operations = array( "squareInt" => "findSquare" ); // operations mapping $securityPolicy = new WSPolicy(file_get_contents('spolicy.xml')); // security policy $securityToken = new WSSecurityToken(array( "user"=>"god", "password"=>"iddqd", "passwordType"=>"PlainText", "ttl" => 100)); // security token $service = new WSService(array( "wsdl"=>"index.wsdl", "operations" => $operations, "serviceName" => "TestService", "policy"=>$securityPolicy, "securityToken"=>$securityToken )); // service instance $service->reply(); ?>
      
      







spolicy.xml



index.wsdl



2つの依存関係がありindex.wsdl



。 WSDLは、標準のWSF / PHPツールによって生成されました-サービスURLに?wsdl



パラメーターを追加します(正しい型などを使用してWSDLを生成する方法については、ドキュメントをもう一度読んでください:この点は非常に賢明に説明されています)。



spolicy.xml



spolicy.xml



ます。 これは、WS-SecurityPolicy仕様に従って記述されたXMLファイルです。 WSPolicyコンストラクターの引数ですべてを記述することは可能でしたが、UsernameTokenはクライアントからの署名を必要とする場合があり、このタスクでは必要ありません。コンストラクター引数はWS-SecurityPolicyの基本的な機能のみを提供します。 さらに、セキュリティの問題がトランスポートまたはHTTPSにあることを発表する必要があります。



 <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702"> <wsp:ExactlyOne> <wsp:All> <sp:TransportBinding> <wsp:Policy> <sp:IncludeTimestamp/> </wsp:Policy> </sp:TransportBinding> <sp:SignedSupportingTokens> <wsp:Policy> <sp:UsernameToken sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient"> <wsp:Policy> <sp:WssUsernameToken10 /> </wsp:Policy> </sp:UsernameToken> <sp:IncludeTimestamp/> </wsp:Policy> </sp:SignedSupportingTokens> </wsp:All> </wsp:ExactlyOne> </wsp:Policy>
      
      







index.wsdl



index.wsdl



domain.tld



は、サービスで使用されるドメインまたはIPに置き換える必要があります)。

 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.wso2.org/php" xmlns:tnx="http://www.wso2.org/php/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:http="http://www.w3.org/2003/05/soap/bindings/HTTP/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://www.wso2.org/php"> <types> <xsd:schema elementFormDefault="qualified" targetNamespace="http://www.wso2.org/php/xsd"> <xsd:element name="squareInt"> <xsd:complexType> <xsd:sequence> <xsd:element name="integer" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="squareIntResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="result" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="squareInt"> <part name="parameters" element="tnx:squareInt"/> </message> <message name="squareIntResponse"> <part name="parameters" element="tnx:squareIntResponse"/> </message> <portType name="TestServicePortType"> <operation name="squareInt"> <input message="tns:squareInt"/> <output message="tns:squareIntResponse"/> </operation> </portType> <binding name="TestServiceSOAPBinding" type="tns:TestServicePortType"> <soap:binding xmlns="http://schemas.xmlsoap.org/wsdl/soap/" transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/" name="squareInt"> <soap:operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="https://domain.tld:443/index.php/squareInt" style="document"/> <input xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/"> <soap:body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/> </input> <output xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/"> <soap:body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/> </output> </operation> </binding> <service name="TestService"> <port xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/" name="TestServiceSOAPPort_Http" binding="tns:TestServiceSOAPBinding"> <soap:address xmlns="http://schemas.xmlsoap.org/wsdl/soap/" location="https://domain.tld:443/index.php"/> </port> </service> </definitions>
      
      







これでサービスの説明は完了です。 すべてが正しく行われている場合、ブラウザでサービスのアドレスを開くと、デプロイされたサービスの説明とその操作のリストが開きます。



お客様


クライアントは、構成で指定されたアドレスをノックして結果を受け取るC#コンソールアプリケーションです。 ログイン、パスワード、および送信された値はハードコードされていますが、この省略を修正することを誰も気にしません。



 internal class Program { // Methods private static void Main() { try { ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) { return true; }; TestServicePortTypeClient client = new TestServicePortTypeClient(); client.ClientCredentials.UserName.UserName = "god"; client.ClientCredentials.UserName.Password = "iddqd"; Console.WriteLine(client.squareInt(5)); } catch (Exception exception) { Console.WriteLine(exception); } finally { Console.ReadKey(); } } }
      
      







構成ファイル( domain.tld



は、サービスで使用されるドメインまたはIPに置き換える必要があります)

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="TestServiceSOAPBinding"> <security mode="TransportWithMessageCredential"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="https://domain.tld/index.php" binding="basicHttpBinding" bindingConfiguration="TestServiceSOAPBinding" contract="MyServiceReference.TestServicePortType" name="TestServiceSOAPPort_Http" /> </client> </system.serviceModel> </configuration>
      
      







まとめ


その結果、コンソールウィンドウに25



が表示されるか、クライアントコードを修正した場合は、手で初期値を入力するために番号の2乗が表示されます。



All Articles