reactjs - 使用 Apache 时 request.headers() 和 request.META 中缺少授权头字段,使用自定义头时浏览器中的 Preflight CORS 错误
问题描述
我在 AWS EC2 实例上启动并运行了我的 Rest Framework API。我已经设置了 Apache 并添加了 SSL 证书。我正在使用我自己的自定义令牌身份验证。
将授权作为标头传递- 在来自Postman 和 React的发布请求时,标头未在
request.headers("Authorization")
and中接收request.META["HTTP_AUTHORIZATION"]
。将 Authorization2 或 x-api-key 作为标头传递-
- 邮递员工作正常
- 在 React 上,浏览器抛出错误
Access to fetch at 'https://www.myapi.live/api/project/add/8/' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization2 is not allowed by Access-Control-Allow-Headers in preflight response.
我已经尝试在 React 中包含 Access-control-origin Header 并在后端设置 django-cors-headers。但这无济于事。
- 将授权作为标头传递,但将 runserver 暴露在
0.0.0.0:8000
而不是 apache https url-
- 在邮递员工作
- 也适用于 React
这是我000-default.conf
的两个sites-enabled
和sites-available
<VirtualHost *:80>
ServerName www.myapi.live
ServerAdmin webmaster@localhost
DocumentRoot /home/ubuntu/django/project
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/ubuntu/django/project/static
<Directory /home/ubuntu/django/project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/django/project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess project python-path=/home/ubuntu/django/project python-home=/home/ubuntu/django/myenv
WSGIProcessGroup project
WSGIScriptAlias / /home/ubuntu/django/project/project/wsgi.py
WSGIPassAuthorization On
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.myapi.live
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</VirtualHost>
解决方案
所以我添加WSGIPassAuthorization On
到我的apache.conf
文件中,该文件位于sites-enabled
. 进行这些更改使其像魅力一样工作,并且 apache 不再剥离Authorization
标题。
以防万一这对将来的其他人有帮助!谢谢!
推荐阅读
- shell - 如何避免在ggsci中删除重复时出现提示
- javascript - 如何在 jQuery click 上预加载下一个背景 css 图像?
- javascript - 在 webgl 中画圆看起来像椭圆
- assembly - 在 x86 程序集中,什么时候应该使用全局变量而不是局部变量?
- docker - 没有 debian 映像可以更新/安装软件包
- r - 'x' 必须是至少二维的数组
- apk - apk中的外部链接问题
- java - 如何有效地处理 JMS 队列避免服务器负载
- python - 当我在列表中有多个纪元时间时,如何在 python 中从 MongoDB 获取数据?
- java - 我为 RSA 加密和解密编写了一个 Java 代码,其中解密密钥太大,因此解密过程需要永远执行