使用ajax请求时某些cookie被跨域阻止

时间:2019-03-10 15:49:01

标签: ajax authentication cookies request header

我正在使用在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发送请求时,该请求不存在。

有人知道我也可以从本地主机发送PHPSESSID吗?

感谢您的任何帮助!

1 个答案:

答案 0 :(得分:0)

在github存储库中提出了同样的问题,所有者解决了。

https://github.com/delight-im/PHP-Auth/issues/154

解决方案:

vendor / delight-im / auth / src / UserManager.php

Replace Session::regenerate(true); with Session::regenerate(true, null);

vendor / 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