Kai*_*ack 17 php jquery google-chrome cross-domain
最近 Chrome 更新到 83.0.4103.116 版本似乎对 Cookie 处理带来了变化。
我为我的用户提供单点登录,让他们登录多个网站。与 Stackoverflow 类似,我正在使用 Jquery执行AJAX 请求:
crossDomain: true,
xhrFields: { withCredentials: true },
Run Code Online (Sandbox Code Playgroud)
在 PHP 中,我允许域:
// needed for cross-domain request
header('Access-Control-Allow-Origin: https://www.example.com');
header('Access-Control-Allow-Credentials: true');
Run Code Online (Sandbox Code Playgroud)
但是,现在它不再起作用了。
在开发控制台中,我发现了一个带有工具提示的新警告:
“由于用户偏好,此 Set-Cookie 已被阻止”
如何解决这个问题?
更新:
我只是看到 Stackoverflow 的单点登录也不再起作用了!
PS:一个相关问题建议告诉您的用户从我的 POV 更改 Chrome 设置,我想避免这种情况。试想一下,通知数百万用户启用 Cookie 进行单点登录……
小智 14
如果您只能在隐身模式中复制这一点,而 Pierre Pretorius 的回答没有帮助,那么您可能会受到 Chrome 83 中的更改的影响,其中默认情况下在隐身模式下会阻止第三方 cookie。请参阅https://angel.co/today/stories/chrome-83-arrives-with-redesigned-security-settings-third-party-cookies-blocked-in-incognito-21796
我认为你不能做太多改变,谷歌打算在未来将此作为默认行为:https : //www.theverge.com/2020/1/14/21064698/google-third-party- cookies-chrome-两年-隐私-safari-firefox
传递 HTTP 标头的站点set-cookie还需要传递SameSiteasNone和also Secure,否则 cookie 不会保存并被忽略。
Set-Cookie: qa_session=...; SameSite=None; Secure
Run Code Online (Sandbox Code Playgroud)
在此之前,请阅读安全隐患: https://blog.heroku.com/chrome-changes-samesite-cookie
PHP 代码示例(来源):
function setcookieSameSite($name, $value, $expire, $path, $domain, $secure, $httponly, $samesite="None")
{
if (PHP_VERSION_ID < 70300) {
setcookie($name, $value, $expire, "$path; samesite=$samesite", $domain, $secure, $httponly);
}
else {
setcookie($name, $value, [
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'samesite' => $samesite,
'secure' => $secure,
'httponly' => $httponly,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18365 次 |
| 最近记录: |