简体   繁体   English

CORS 使用 S3 Bucket 和 CloudFront 的严格来源

[英]CORS Strict Origin using S3 Bucket and CloudFront

I'm using a S3 Bucket with a CloudFront distribution on a sub domain but I get strict-origin-when-cross-origin on a GET request.我在子域上使用带有 CloudFront 分布的 S3 存储桶,但我在 GET 请求中得到了 strict-origin-when-cross-origin。 I can't see what I've done wrong so any help is greatly appreciated.我看不出我做错了什么,所以非常感谢任何帮助。 My website url is https://www.project1.tompenn.co.uk/ And my content url is https://content.tompenn.co.uk/TFTSet7_2/traits.json我的网站 url 是https://www.project1.tompenn.co.uk/我的内容 url 是https://content.tompenn.co.uk/TFTSet7_2/traits.json

Developer Console on my website is throwing the error: Access to fetch at 'https://content.tompenn.co.uk/TFTSet7/traits.json' from origin 'https://www.project1.tompenn.co.uk' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.我网站上的开发人员控制台抛出错误: Access to fetch at 'https://content.tompenn.co.uk/TFTSet7/traits.json' from origin 'https://www.project1.tompenn.co.uk' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

在此处输入图像描述

Here is the CORS policy on my S3 Bucket:这是我的 S3 存储桶上的 CORS 政策: 在此处输入图像描述

Here is how my CloudFront Distribution is setup.以下是我的 CloudFront Distribution 的设置方式。 I only have the one behaviour and this is the config for it:我只有一种行为,这是它的配置: 在此处输入图像描述 在此处输入图像描述

Okay changing this option seems to have solved my issue.好的,更改此选项似乎解决了我的问题。 在此处输入图像描述

I had a very similar issue and after hours of banging my head against the wall I found that something with Chrome's HTTP caching mechanism prevents the Origin header from being sent.我遇到了一个非常相似的问题,在用头撞墙数小时后,我发现 Chrome 的 HTTP 缓存机制阻止了Origin header 的发送。 This is a Chrome-specific issue as I could not reproduce it with Safari. You can check whether this is the case for you as well by toggling the "Disable Cache" option under the Network tab of Chrome developer tools .这是一个特定于 Chrome 的问题,因为我无法使用 Safari 重现它。您可以通过切换Chrome 开发人员工具的网络选项卡下的“禁用缓存”选项来检查您是否也是这种情况。

To force your request to ignore the cache, use appropriate cache option ( documentation ).要强制您的请求忽略缓存,请使用适当的cache选项(文档)。 This is my final working code:这是我的最终工作代码:

fetch(url, {
  method: 'GET',
  mode: 'cors',
  cache: 'no-store', // for some reason Chrome's caching doesn't send Origin
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM