0

我正在尝试通过使用 go lang 在 KrakneD 中添加自定义逻辑来构建 http 插件。但目前我从 KrakenD 收到 500 个内部服务器错误,在后端收到 401 个未经授权的错误。当我调试更多时,我可以看到不记名令牌没有传递到后端。

KrakenD 后端配置:

"backend": [
        {
            "url_pattern":  "My downstream Path",
            "method":  "Http Method",
            "host": [
                 "My Host"
            ],
            "extra_config": {
                "github.com/devopsfaith/krakend/transport/http/client/executor": {
                    "name": "Plugin Register Name"
                },
                "github.com/devopsfaith/krakend-oauth2-clientcredentials": {
                    "endpoint_params": {},
                    "token_url": "My Token URL",
                    "client_id": "My Client ID",
                    "client_secret": "My Client Secret"
                }
            },
            "disable_host_sanitize": false
        }
]

Go Lang 插件

func (r registerer) registerClients(ctx context.Context, extra map[string]interface{}) (http.Handler, error) {
    name, ok := extra["name"].(string)
    if !ok {
        return nil, errors.New("wrong config")
    }
    if name != string(r) {
        return nil, fmt.Errorf("unknown register %s", name)
    }

    // return the actual handler wrapping or your custom logic so it can be used as a replacement for the default http client
    return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

        fmt.Println(req.Header.Get("Authorization")) // Bearer token is coming empty. I am expecting bearer token value here, which was configured in KrakenD

        client := &http.Client{
            Timeout: time.Second * 10,
        }
        resp, err := client.Do(req)
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        w.Write(body)
    }), nil
}
4

0 回答 0