Отправить базовую авторизацию в балерине с помощью модуля wso2 / soap - PullRequest
0 голосов
/ 16 ноября 2018

Я использую балерину и хочу подключиться к серверу идентификации WSO2 для аутентификации.

Я не могу добавить базовую авторизацию с помощью wso2 / soap.

Может ли кто-нибудь привести пример?

   xml body = xml `<tes:insert_employee_operation xmlns:tes="http://teste.cv">
     <tes:name>{{username}}</tes:name>
     <tes:age>10</tes:age>
     <tes:ssn>25</tes:ssn>
  </tes:insert_employee_operation>`;


soap:SoapRequest soapRequest = {
    soapAction: "urn:insert_employee_operation",
    payload: body
};

io:println(soapRequest);

var details = soapClient->sendReceive("/services/EmployeeService", soapRequest);
match details {
    soap:SoapResponse soapResponse => {
        io:println(soapResponse);
         xml respostaXml = soapResponse.payload;
         json respostaJson = respostaXml.toJSON({});
         response.setJsonPayload(respostaJson);
         _=caller->respond(response);

        }
    soap:SoapError soapError => io:println(soapError);
}

код

Ответы [ 2 ]

0 голосов
/ 19 ноября 2018

Вы можете добавить базовую авторизацию в конфигурации конечной точки клиента.

endpoint soap:Client soapClient {
        clientConfig: {
            url: "http://localhost:9000",
            auth: {
                scheme: http:BASIC_AUTH,
                username: "is_username",
                password: "is_password"
            }
        }
    };

Это добавит заголовок Authorization в HTTP-запрос.Полный код будет выглядеть следующим образом:

import ballerina/http;
import ballerina/io;
import ballerina/log;
import wso2/soap;

endpoint soap:Client soapClient {
    clientConfig: {
        url: "http://localhost:9000",
        auth: {
            scheme: http:BASIC_AUTH,
            username: "is_username",
            password: "is_password"
        }
    }
};

public function main(string... args) {
    xml body = xml `<tes:insert_employee_operation xmlns:tes="http://teste.cv">
                        <tes:name>{{username}}</tes:name>
                        <tes:age>10</tes:age>
                        <tes:ssn>25</tes:ssn>
                    </tes:insert_employee_operation>`;


    soap:SoapRequest soapRequest = {
        soapAction: "urn:insert_employee_operation",
        payload: body
    };

    io:println(soapRequest);

    var details = soapClient->sendReceive("/services/EmployeeService", soapRequest);
    match details {
        soap:SoapResponse soapResponse => {
            io:println(soapResponse);
            xml respostaXml = soapResponse.payload;
            json respostaJson = respostaXml.toJSON({});
            response.setJsonPayload(respostaJson);
            _ = caller->respond(response);

        }
        soap:SoapError soapError => io:println(soapError);
    }
}
0 голосов
/ 19 ноября 2018

В объекте soap:SoapRequst доступно больше полей. Смотри https://central.ballerina.io/wso2/soap#SoapRequest.

Если вы имели в виду ws-security, то можете использовать следующее:

soap:SoapRequest soapRequest = {
    soapAction: "urn:insert_employee_operation",
    payload: body,
    username: "foo",
    password: "bar"
};

Вы также можете установить заголовки конвертов мыла, используя поле заголовков.

...