AWS Lambda 和设置 cookie

发布于 2025-01-14 20:07:40 字数 937 浏览 2 评论 0原文

我在 lambda 函数的响应上设置 cookie 时遇到问题。

有两个页面(上传到 s3 存储桶)- 登录和验证。

在我的登录页面上,我对第三个 api 进行了一些调用,最后我返回了一个 cookie。

{
  statusCode: 200,
  headers: {
    'Access-Control-Allow-Origin': 'HERE GOES DOMAIN WITH LOGIN.HTML AND VALIDATE.HTML FILES',
    'Access-Control-Allow-Credentials': true,
    'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,X-Amz-Security-Token,Authorization,X-Api-Key,X-Requested-With,Accept,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers',
    'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,HEAD,OPTIONS',
    'Set-Cookie': "sess_id=key;Expires=SOME_VAL;Path=/"
  }
}

我确实在响应标头中的开发人员工具中看到了该 cookie,但由于某种原因它没有被设置。 document.cookie 结果带有空字符串。

因此,我手动进行了设置,但是当我在第二页(验证)上对 API 网关(用于验证该会话的 lambda)执行另一个请求时,会话不包含在请求标头中。

我对每个请求使用 fetch web api 和 credentials:"include" 但没有成功。

有什么建议吗? 谢谢。

I have a problem with setting a cookie on the response from the lambda function.

There are two pages (uploaded on s3 bucket) - login and validate.

On my login page I do some calls to 3rd api and at the end of that I return a cookie.

{
  statusCode: 200,
  headers: {
    'Access-Control-Allow-Origin': 'HERE GOES DOMAIN WITH LOGIN.HTML AND VALIDATE.HTML FILES',
    'Access-Control-Allow-Credentials': true,
    'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,X-Amz-Security-Token,Authorization,X-Api-Key,X-Requested-With,Accept,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers',
    'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,HEAD,OPTIONS',
    'Set-Cookie': "sess_id=key;Expires=SOME_VAL;Path=/"
  }
}

I do see that cookie in developers tool in response headers but its not getting set up for some reason. document.cookie result with empty string.

So I set that up manually but when I do another reqeust on my second page (validate) to API gateway (lambda for validating that session) the session is not included in the request headers.

I use fetch web api and credentials:"include" with each request but without any success.

Any tips?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2025-01-21 20:07:40

你的回答看起来是正确的。
Cookie 只会发送到它们所来自的域。
您确定您的 cookie 来自同一个域吗?
例如,如果 cookie 来自 localhost:4001,前端是 localhost:4000,则不会保存 Cookie。

Your response looks correct.
Cookies are only going to go to the domains from which they came.
Are you sure your cookie came from the same domain?
Cookie won't be saved if for example cookies came from localhost:4001, the frontend is localhost:4000.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文