ajax - 使用 ajax 请求时,某些 cookie 被跨域阻止
问题描述
我正在使用在 localhost:3000 上运行的 react 应用程序,它向我们的网站发出 ajax 请求。我们最近将我们的身份验证系统从使用 WordPress 身份验证切换到https://github.com/delight-im/PHP-Auth。
从那时起,在 ajax 和我们的 Web 服务器上使用相同的设置,我们的身份验证 cookie 不会跨域发送。但是,它在从同一个域请求它们时起作用。
我们的要求:
fetchLoginStatus = () => {
const ajax = new XMLHttpRequest();
ajax.withCredentials = true;
ajax.open("POST", "https://our-website.com/src/php/checkLoggedIn.php");
ajax.onload = () => {
const response = JSON.parse(ajax.responseText);
};
ajax.send();
};
我们的请求标头(来自 localhost:3000):
:authority: my-website.com
:method: POST
:path: /src/php/checkLoggedIn.php
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
content-length: 0
cookie: plesk-items-per-page; plesk-sort-field, phpMyAdmin; databases-active-list-state-collapsed; plesk-list-type; io=R_dL3fjUEYe64ykHAAAp; isAsyncProgressBarCollapsed=true; PLESKSESSID; plesk-sort-dir;
origin: https://localhost:3000
referer: https://localhost:3000/
我们的响应头(我们正在运行一个 nginx 服务器):
access-control-allow-credentials: true
access-control-allow-headers: origin, x-requested-with, content-type
access-control-allow-methods: PUT, GET, POST, DELETE, OPTIONS access-`
control-allow-origin: https://localhost:3000
cache-control: no-store, no-cache, must-revalidate
content-encoding: br
content-type: text/html; charset=UTF-8
date: Sun, 10 Mar 2019 15:26:08 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT pragma:
no-cache server: nginx
set-cookie: PHPSESSID=someId;
path=/; SameSite=Lax status: 200
vary: Accept-Encoding
x-powered-by: PleskLin`
当我不发送请求时,跨域 PHPSESSID 在我的请求标头的 cookie 中。但是,当我从 localhost:3000 发送请求时,它不存在。
有人知道我也可以从 localhost 发送 PHPSESSID 吗?
提前感谢您的帮助!
解决方案
在github存储库中问了同样的问题,所有者解决了它。
https://github.com/delight-im/PHP-Auth/issues/154
解决方案:
供应商/delight-im/auth/src/UserManager.php
Replace Session::regenerate(true); with Session::regenerate(true, null);
供应商/delight-im/auth/src/Auth.php
Replace @Session::start(); with @Session::start(null);
Replace Session::regenerate(true); with Session::regenerate(true, null);
After $cookie->setSecureOnly($params['secure']); append $cookie-
>setSameSiteRestriction(null); in all three (!) occurrences
推荐阅读
- java - 无法将流图像解码为片段
- python - 如何在 Visual Studio Code 中显示 Pylint 分数?
- flutter - 现在有两种在 Dart 中使用 typedef 的方法吗?
- python - 用户闲置 60 秒后超时输入(提示)[python]
- view - SwiftUI - 从 ObservableObject 触发模态表?
- c++ - 如何从 C++ 中的向量中删除不连续的元素
- ios - Big Sur、React Native 和 launchPackager.command
- c# - 如何从 C# 中的枚举中获取整数值
- json - 在 Airflow 中解析 JSON 字符串
- node.js - NodeJs 缓冲区未附加到 Nodemailer 文件