- Chrome 51 開始,瀏覽器的 Cookie 新增加了一個SameSite屬性,用來防止 CSRF 攻擊和用戶追蹤。
- Chrome升級到80版本后,默認限制了cross-site攜帶cookie,導致cookie失效,報錯如下
A cookie associated with a cross-site resource at http://XXX.XXX.XXX.XXXX/ was set without the `SameSite` attribute.
It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`.
解決方案1
在cookie中追加屬性 secure; SameSite=None
- 此方案需要使用https協議
- 此方案可能由於某些瀏覽器不支持SameSite屬性而使cookie無法正確傳遞,需要判斷user_agent包含chrome才追加此屬性
- 使用nginx根據user_agent自動追加samesite屬性
http {
...
map $http_user_agent $samesite_attr {
"~*chrome" ';Secure;SameSite=None';
}
...
server {
location / {
...
proxy_cookie_path ~/(.*) "/$1$samesite_attr";
}
}
}
解決方案2
Chrome訪問地址 chrome://flags/
搜索"SameSite",修改配置項如圖