import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:OutboundBasicAuthProvider outboundBasicAuthProvider = new ({
username: "tom",
password: "1234"
});
http:BasicAuthHandler outboundBasicAuthHandler =
new (outboundBasicAuthProvider);http:Client httpEndpoint = new ("https://localhost:9090", {
auth: {
authHandler: outboundBasicAuthHandler
},
secureSocket: {
trustStore: {
path: config:getAsString("b7a.home") +
"/bre/security/ballerinaTruststore.p12",
password: "ballerina"
}
}
});public function main() {
var response = httpEndpoint->get("/hello/sayHello");
if (response is http:Response) {
var result = response.getTextPayload();
log:printInfo((result is error) ? "Failed to retrieve payload."
: result);
} else {
log:printError("Failed to call the endpoint.", response);
}
}# To test the client, first start the sample service by executing the below
# command by specifying the configuration file name and passing Ballerina home
# path as a system property.
ballerina run secured_service_with_basic_auth.bal --b7a.config.file=sample-users.toml --b7a.home=<ballerina_home_path>
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9090# Then start the client by executing the below command by passing Ballerina home
# path as a system property.
ballerina run secured_client_with_basic_auth.bal --b7a.home=<ballerina_home_path>
INFO [ballerina/log] - Hello, World!!!import ballerina/auth;
import ballerina/config;
import ballerina/http;
import ballerina/log;
auth:InboundBasicAuthProvider inboundBasicAuthProvider = new;
http:BasicAuthHandler inboundBasicAuthHandler = new (inboundBasicAuthProvider);listener http:Listener ep = new (9090, config = {
auth: {
authHandlers: [inboundBasicAuthHandler],
scopes: ["hello"]
},
secureSocket: {
keyStore: {
path: config:getAsString("b7a.home") +
"/bre/security/ballerinaKeystore.p12",
password: "ballerina"
}
}
});service hello on ep {
resource function sayHello(http:Caller caller, http:Request req) {
error? result = caller->respond("Hello, World!!!");
if (result is error) {
log:printError("Error in responding to caller", result);
}
}
}
Secured Client with Basic AuthA client, which is secured with Basic authentication should be used to
connect to a service, which is secured with Basic authentication.
The |
|
|
|
Defines the Basic Auth client endpoint to call the backend services.
Basic Authentication is enabled by creating an
|
|
Creates a Basic Auth handler with the created Basic Auth provider. |
|
|
|
|
|
Send a |
|
|
|
|
|
|
Defines the sample backend service, which is secured with Basic Auth authentication. |
|
|
|