【问题标题】:aws cognito && apiGateway returns status 401aws cognito && apiGateway 返回状态 401
【发布时间】:2018-03-23 21:13:09
【问题描述】:

我在我的 api 的响应头中正确地进行了 cors 配置。

当我通过邮递员使用经过验证的令牌测试我的 api 时 附加在 header('Authorization') 中,它返回 200。

我检查了我的前端获取代码以请求该 api,似乎没有错误或故障。

这怎么可能发生?有没有人和我现在一样苦苦挣扎。

添加:

我的前端提取代码如下所示。

export const getDoc = async (docId, token) => {
    const path = `${apiGateway.URL}`;
    const body = {
        docId: docId
    };
    const headers = {
        Authorization: token,
        'Content-Type': 'application/json'

    };
    const result = await fetch(path, {
        body,
        headers,
    });
    if (result.status !== 200) {
        throw new Error('failed to get doc');
    }
    return result.json();
};

【问题讨论】:

  • @VijayanathViswanathan 当然,我已经在 API 网关中完成了基本的 CORS 配置。正如我上面提到的,邮递员的 API 调用成功。但它在我的前端代码中不起作用
  • “授权”请求标头和“内容类型:应用程序/json”都会触发您的浏览器进行预检。所以问题正是我在之前的评论中描述的:您需要重新配置服务器以不需要对 OPTIONS 请求进行授权。原因是,浏览器在发出预检 OPTIONS 请求时不会发送 Authorization 请求标头。之所以不这样做是因为该 OPTIONS 请求的全部目的是让浏览器询问服务器,您是否允许具有 Authorization 标头的跨域请求?

标签: amazon-web-services cors aws-api-gateway amazon-cognito


【解决方案1】:

您应该在“Token Source”字段中输入“Authorization”,而不是“method.request.headers.Authorization”。否则会出现 401 错误。

【讨论】:

  • 你能解释更多吗
猜你喜欢
  • 2017-01-26
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2020-03-20
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多