/opt/wso2/wsf_c
).findSquare
, which, in fact, performs our task; associating the operation name with the function name, declaring the security policy and creating a token (in this example, the login and password are hardcoded, but using the passwordCallback
argument in the WSSecurityToken constructor (see API ), you can specify a function that can, for example, give it a login database password, and return it for further verification); Creation of a WSService instance with all parameters and call processing.index.php
file <?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
and index.wsdl
. WSDL was generated by the WSF / PHP tool by adding the ?wsdl
parameter to the service URL (how to generate WSDL with the correct types, etc., again, read the documentation: this point is described quite sensibly there).spolicy.xml
. This is an XML file written in accordance with the WS-SecurityPolicy specification. It was possible to describe everything in the arguments of the WSPolicy constructor, but for some time the UsernameToken required a signature from the client, which we do not need in this task, and the constructor arguments provide only the basic WS-SecurityPolicy capabilities. In addition, we need to announce that security issues lie on transport, that is, 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
( domain.tld
should be replaced with the domain or IP used by the service): <?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>
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
should be replaced with the domain or IP used by the service): <?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
should appear in the console window, or, if you have corrected the client code in order to enter the initial value with your hands, the square of your number.Source: https://habr.com/ru/post/132353/
All Articles