【发布时间】:2019-10-05 07:29:15
【问题描述】:
我在我的文本字段中使用 django-summernote。这应该使我的文本字段看起来有点像下面的图片
现在上面的静态文件存储在我的 AWS S3 存储桶中。我在浏览器控制台中收到403 error,下面是我的文本字段现在的样子
跨域请求被阻止:同源策略不允许读取位于https://some_bucket_66d.s3.amazonaws.com/static/summernote/font/summernote.woff?1d9aeaaff0a8939558a45be6cd52cd4c 的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。[了解更多] 可下载字体:下载失败(字体系列:“summernote”样式:正常重量:400 拉伸:100 src 索引:1):URI 错误或不允许跨站点访问来源:https://some_bucket_6d.s3.amazonaws.com/static/summernote/font/summernote.woff?1d9aeaaff0a8939558a45be6cd52cd4c
所以为了解决这个错误我做了
pip install django-cors-headers
添加
INSTALLED_APPS = (
...
'corsheaders',
...
]
将其添加到我的中间件
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
...
]
并在我的 django 设置中将以下 3 个链接添加到我的白名单中。我不知道http://127.0.0.1:9000 是做什么用的,但我还是让它在那里,就像在https://pypi.org/project/django-cors-headers/ 页面中一样
CORS_ORIGIN_WHITELIST = [
"https://some_bucket_66d.s3.amazonaws.com", #This is the bucket path as you see in the error above
"http://localhost:8080",
"http://127.0.0.1:9000"
]
即使在 Django Cors 中将其列入白名单后,我仍然遇到同样的错误我做错了什么以及如何解决它
尝试了@jusrDare 建议的解决方案。现在错误消息已更改为以下
downloadable font: download failed (font-family: "summernote" style:normal weight:400 stretch:100 src index:1): status=2147746065 source: https://some_bucket_66d.s3.amazonaws.com/static/summernote/font/summernote.woff?1d9aeaaff0a8939558a45be6cd52cd4c
另外,如果您不想将 AllowedOrigin 设置为 *,您可以尝试以下代码
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://www.your-site.com</AllowedOrigin>
<AllowedOrigin>https://www.your-site.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
</CORSRule>
</CORSConfiguration>
他们俩都给我同样的错误
【问题讨论】:
-
添加 CORS 允许其他网站访问您的内容;它不允许您访问其他网站的内容。所以这些都不会产生任何影响。我对这个软件包一无所知,但您可能在某个地方遇到了安装或配置错误。如果你认为包有bug,可以在Github页面file an issue。
标签: django python-3.x amazon-web-services amazon-s3 django-cors-headers