1

我在 ballerinalang 中实现了一个名为https://localhost:9090/isValidUser. 下面是我的代码

import ballerina.net.http;

@http:configuration {
    basePath:"/",
    httpsPort:9090,
    keyStoreFile:"${ballerina.home}/bre/security/wso2carbon.jks",
    keyStorePassword:"wso2carbon",
    certPassword:"wso2carbon",
    trustStoreFile:"${ballerina.home}/bre/security/client-truststore.jks",
    trustStorePassword:"wso2carbon"
}
service<http> authentication {
    @http:resourceConfig {
        methods:["POST"],
        path:"/isValidUser"
    }

    resource isValidUser (http:Request req, http:Response res) {
        println(req.getHeaders());
        res.send();

    }
}

现在我需要做的是,当我从浏览器调用该 URL 时,我需要将用户重定向到另一个https://localhost:3000在我的服务中发生一些验证后调用的 URL。

那么我怎样才能从芭蕾舞演员中进行这种重定向呢?

4

2 回答 2

1

Ballerina 提供了流畅的 API 来进行重定向。请检查以下详细说明侦听器端点重定向的代码。

service<http:Service> redirect1 bind {port:9090} {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }
    redirect1 (endpoint client, http:Request req) {
        http:Response res = new;
        _ = client -> redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}

Ballerina Redirects中提供了完整的示例

于 2018-05-25T08:51:28.047 回答
0

在 Ballerina 中,您需要通过设置必要的标头和状态代码来自己处理重定向。以下示例是一个简单的演示,展示了如何在 Ballerina 中重定向。(注意:我在 Ballerina 0.95.2 中试过这个)

import ballerina.net.http;

@http:configuration {basePath:"/hello"}
service<http> helloWorld {

    @http:resourceConfig {
        methods:["GET"],
        path:"/"
    }
    resource sayHello (http:Request request, http:Response response) {
        map qParams = request.getQueryParams();
        var name, _ = (string)qParams.name;

        if (isExistingUser(name)) {
            response.setStringPayload("Hello Ballerina!");
        } else {
            response.setHeader("Location", "http://localhost:9090/hello/newUser");
            response.setStatusCode(302);
        }

        _ = response.send();
    }

    @http:resourceConfig {path:"/newUser"}
    resource newUser (http:Request request, http:Response response) {
        string msg = "New to Ballerina? Welcome!";
        response.setStringPayload(msg);
        _ = response.send();
    }
}

function isExistingUser (string name) (boolean) {
    return name == "Ballerina";
}
于 2017-11-30T10:17:28.333 回答