【问题标题】:No X-Endpoint-API-UserInfo header forwarded by Extensible Service Proxy可扩展服务代理没有转发 X-Endpoint-API-UserInfo 标头
【发布时间】:2019-10-30 03:26:37
【问题描述】:

我正在使用 Google Cloud Endpoints 和 Google Cloud Functions 设置一个宠物项目,并使用 Google ID 令牌身份验证。我已经按照 thisthis 指南进行设置,但是 X-Endpoint-API-UserInfo 标头未传递给云函数。

我在this OpenAPI yaml 中设置了一个 POST 端点。它部署到 Cloud Endpoints,也是一个可扩展的服务代理。当我使用 gcloud auth application-default print-access-token 生成的令牌从 Postman 调用它时,我发现身份验证确实有效,但函数中只存在以下标头:host, user-agent, transfer-encoding, accept, accept-encoding, authorization, cache-control, forwarded, function-execution-id, postman-token, x-appengine-city, x-appengine-citylatlong, x-appengine-country, x-appengine-default-version-hostname, x-appengine-https, x-appengine-region, x-appengine-request-log-id, x-appengine-user-ip, x-cloud-trace-context, x-forwarded-for, x-forwarded-proto, x-real-ip, connection

你能帮我为什么我没有从函数中的 ESP 获取用户信息吗?

【问题讨论】:

  • 嘿@esgott,我已经尝试过 Wayne Zhang 的解决方案,它对我有用。你能确认它是否适合你,这就是你真正想做的吗?
  • 有意思,我刚刚又试了一遍,认证不了了。我收到401 Unauthorized 和消息JWT validation failed: Bad JWT format: Invalid JSON in header。我想知道我生成访问令牌的方式是否良好。
  • 如果出现错误,请确保您的 JWT 包含有效的 JSON。 Here 你可以找到一个有效的解码 JWT 令牌的例子。

标签: google-cloud-functions google-cloud-endpoints openapi


【解决方案1】:

我刚刚验证了 ESP 确实将 X-Endpoint-Api-Userinfo 标头发送到后端函数。

这是我的后端代码

cat hello.go 
// Package helloworld provides a set of Cloud Functions samples.
package helloworld

import (
        "encoding/json"
        "fmt"
        "html"
        "net/http"
)

// HelloHTTP is an HTTP Cloud Function with a request parameter.
func HelloHTTP(w http.ResponseWriter, r *http.Request) {
        for key, vals := range r.Header {
                for _, val := range vals {
                        resp := fmt.Sprintf("request-headers: %s=%s\n", key, val)
                        w.Write([]byte(resp))
                }
        }
        var d struct {
                Name string `json:"name"`
        }
        if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
                fmt.Fprint(w, "Hello, World!")
                return
        }
        if d.Name == "" {
                fmt.Fprint(w, "Hello, World!")
                return
        }
        fmt.Fprintf(w, "Hello, %s!\n", html.EscapeString(d.Name))
}

这是来自后端的标头

request-headers: X-Appengine-Country=ZZ
request-headers: X-Appengine-Default-Version-Hostname=gef3fd9f75048d933-tp.appspot.com
request-headers: X-Appengine-Request-Log-Id=5db8910800ff043c9b1b0b1d300001737e67656633666439663735303438643933332d7470000161313837383661623961353931326664363534373361386233343137366433633a33000100
request-headers: X-Endpoint-Api-Userinfo=deducted
request-headers: X-Forwarded-Proto=https
request-headers: X-Real-Ip=100.117.29.55
request-headers: Connection=close
request-headers: User-Agent=curl/7.64.0
request-headers: Accept=*/*
request-headers: Authorization=Bearer deducted
request-headers: Forwarded=for="100.117.29.55";proto=https,for="107.178.206.195";proto=https
request-headers: X-Appengine-Https=on
request-headers: X-Appengine-User-Ip=107.178.206.195
request-headers: X-Endpoint-Api-Project-Id=402804073044
request-headers: X-Forwarded-For=100.117.29.55, 100.117.29.55,107.178.206.195
request-headers: Accept-Encoding=gzip
request-headers: Function-Execution-Id=fea3ox4erxsd
request-headers: X-Cloud-Trace-Context=db9bb39b72286dd6282f5319a18f72c8/9278156698602755161;o=1

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 2015-08-09
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2022-10-19
    相关资源
    最近更新 更多