django - 403 响应 Ionic 4 + Django Api 在同一台服务器上?
问题描述
我构建了一个使用 Django API REST 端点的 Ionic 4 应用程序。
该应用程序已在 iPad 和我的本地主机上使用 ionic-app-scripts serve 进行了测试。
现在我想将它部署为 web 应用程序,我通过为浏览器平台构建(ionic cordova build browser --prod --release)并在 API REST 所在的同一个 Django 应用程序上提供 index.html 来做到这一点开启。静态文件(js、资产等,托管在 Amazon S3 存储桶上)
现在在 iPad 和 localhost 上,API REST POST 请求都没有问题。但是当我在实时服务器上运行应用程序时(即使请求是从与 API REST 相同的服务器发送的!!)我得到 403...
当我比较请求标头时,我看到的唯一区别是在实时服务器上发送的标头没有“Origin”参数:
在实时服务器上:
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 62
content-type application/json
Cookie _ga=GA1.2.1002502516.149305462…eay6lysy1w44vawa94lug20hpek2n
Host example.com
Pragma no-cache
Referer http://example.com/front/
User-Agent Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/60.0
在本地主机上:
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Content-Length 62
content-type application/json
Host example.com
Origin http://localhost:8100
Referer http://localhost:8100/
User-Agent Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/60.0
有任何想法吗?我知道关于这个主题有几个问题和帖子,但通常问题是相反的(从另一个域发送帖子时出现 CORS 问题)
(显然 example.com 不是我正在使用的真实域)
解决方案
发现了问题,我有一个 Django 身份验证系统,并使用它登录。当我注销时,它工作正常。似乎它在请求中发送带有 cookie 的 csrf 令牌,这导致了冲突。
推荐阅读
- gradle - 在 build.gradle 中更改胶水
- dc.js - 使用 DC js 和 Crossfilter 以及数据表的多个饼图
- reverse-engineering - google cast 协议 v2 的身份验证响应消息中字段 7 的类型
- reactjs - 理解 Formik 和 React
- c - 我可以在 C 中获取 RIP 地址吗?
- c - 在 C 中相互调用的 2 个内联函数的行为
- css - 将 className 属性添加到 dangerouslySetInnerHTML 内容
- javascript - 我在表格行上的分配没有反映在前端
- c# - 当 nuget 包依赖于非 nuget 程序集时,如何发布它
- javascript - 如何验证 p、li、div 元素文本