gpt4 book ai didi

对需要身份验证的云运行服务的 Ajax 请求

转载 作者:行者123 更新时间:2023-12-05 00:45:46 25 4
gpt4 key购买 nike

我在一个服务上运行谷歌云时遇到了与 CORS 相关的问题
需要身份验证。

如果我尝试通过 cli 使用 Bearer token 执行 curl 命令,
一切正常。
不幸的是,如果我尝试在 javascript 中通过 ajax 执行相同的调用,
我收到了 403。

  const http = new XMLHttpRequest();
const url = 'https://my-app.run.app';

http.open("GET", url);
http.withCredentials = true;
http.setRequestHeader("authorization", 'Bearer ' + id_token);
http.send();
http.onreadystatechange = (e) => {
console.log(http.responseText)
}

云运行日志中的错误是这样的:
The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header. Read more at https://cloud.google.com/run/docs/securing/authenticating

容器永远不会被击中。

我看到的问题是,当我在网络中使用 ajax 进行调用时
浏览器。 Web 浏览器正在发出飞行前请求(
url )而不发送授权 header (这是预期的
行为 )

问题似乎是云运行尝试对 OPTIONS 进行身份验证
请求并且永远不会到达我的容器,据我所知,
不应该这样做。 (
https://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0 )

这是云运行的已知问题吗?

如何向经过身份验证的云运行服务发出 ajax 请求?

最佳答案

(云跑 PM)

这是一个已知的问题。有几个选项:

  • 允许未经身份验证的请求并自己执行 CORS/auth
  • 有一个使用 Cloud Endpoints running on Cloud Run 的变体。在你的电脑前。让 Endpoints 执行您的最终用户身份验证,然后将请求转发到您的后端。
  • 从同一域提供服务(例如,使用 Firebase Hosting proxy )

  • 我们已经考虑实现 Istio CORSPolicy ,这将在身份验证检查之前返回 CORS header ,尽管到目前为止我们还没有 promise 这样做。

    关于对需要身份验证的云运行服务的 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872735/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com