美文网首页
新版本chrome浏览器带来的跨域请求cookie丢失问题

新版本chrome浏览器带来的跨域请求cookie丢失问题

作者: 硫克的苹果 | 来源:发表于2020-04-23 10:28 被阅读0次

近期业务线上出现问题,嵌套的页面莫名丢失cookie;
经深入排查,发现新版本的chrome浏览器(80版本之后)对cookie的校验更加严格,有页面嵌套的可能会出现问题。
chrome升级到80版本之后(最坑的地方:灰度测试,即也可能存在同一版本不同人的浏览器表现不同),cookie的SameSite属性默认值由None变为Lax,该问题的讨论可参考:https://github.com/google/google-api-javascript-client/issues/561

在Lax模式下,以下类型请求将受影响:

请求类型 示例 正常情况 Lax
链接 <a href="..."></a> 发送 Cookie 发送 Cookie
预加载 <link rel="prerender" href="..."/> 发送 Cookie 发送 Cookie
GET 表单 <form method="GET" action="..."> 发送 Cookie 发送 Cookie
POST 表单 <form method="POST" action="..."> 发送 Cookie 不发送
iframe <iframe src="..."></iframe> 发送 Cookie 不发送
AJAX $.get("...") 发送 Cookie 不发送
Image <img src="..."> 发送 Cookie 不发送

解决方法1
下面的设置无效。
Set-Cookie: widget_session=abc123; SameSite=None
下面的设置有效。
Set-Cookie: widget_session=abc123; SameSite=None; Secure
解决方法2
两个跨域的网站部署到一起,例如
www.xxx.com/A网站
www.xxx.com/B网站
解决问题3:
谷歌浏览器里面:
chrome://flags/
把SameSite by default cookies这个参数设置成disabled
解决方法4
修改应用服务器方式
1.tomcat:

首先你需要在httpd.conf文件中开启mod_header模块
LoadModule headers_module modules/mod_headers.so
你打开它以后并不能生效,所以我们还要重写Set-Cookie的头信息,其他的方法我们已经尝试并不可行,目前只有这一种方法可行
Header always edit "Set-Cookie" "path=/" "path=/; Secure; SameSite=None"
对应windows下的apache
Header edit "Set-Cookie" "path=/" "path=/; Secure; SameSite=None"
对应linux下的apache
这里修改完有些人可能不生效,关键字就是always 如果你加了always不生效就去掉,反之则加上
2.nginx

在 nginx 的 location 中配置
proxy_cookie_path / "/; httponly; secure; SameSite=None";

示例

server {
    listen 443 ssl http2;
    server_name www.cat73.org;

    ssl_certificate /etc/letsencrypt/live/cat73.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cat73.org/privkey.pem;

    ssl_trusted_certificate /etc/letsencrypt/live/cat73.org/chain.pem;

    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options SAMEORIGIN;
    add_header Strict-Transport-Security "max-age=15768000";

    location / {
        root /var/www/html;
    }

    location /api {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 在这里设置
        proxy_cookie_path / "/; httponly; secure; SameSite=Lax";
    }
}

相关文章

  • 新版本chrome浏览器带来的跨域请求cookie丢失问题

    近期业务线上出现问题,嵌套的页面莫名丢失cookie;经深入排查,发现新版本的chrome浏览器(80版本之后)对...

  • chrome浏览器跨域请求cookie丢失问题

    这里的问题,就是在包括getUserInfo之后的接口中Request Header中都丢失了cookie,当时一...

  • [FE] Chrome 跨域请求失败:This Set-Cook

    问题 Chrome 更新到 80 以后,本地发起的跨域请求失败了。 This Set-Cookie didn't ...

  • localStorage、sessionStorage、Cook

    用一张表格形象简单说明: 说明:1:关于cookie存在的跨域问题当浏览器向后台发起请求时,如果是跨域请求,那么就...

  • cookies/ session / token概念

    cookies session session与cookie的区别 cookie跨域请求携带如何解决 浏览器同源策...

  • 跨域设置整理

    什么是跨域 不同域名之间相互请求资源,就是跨域。常说的跨域问题,指的是浏览器不允许跨域请求。这是由于浏览器的同源策...

  • cookie 跨域访问的解决方案

    资料: 理解Cookie和Session机制 Ajax跨域请求中的Cookie问题(默认不带cookie等凭证) ...

  • Chrome浏览器的跨域问题

    解决Chrome浏览器的跨域问题 对于前端来说,本地开发很经常会遇到跨域问题,最简单的方法就是将浏览器设置成可跨域...

  • 解决跨域问题

    概述 在浏览器端进行 Ajax 请求时会出现跨域问题,那么什么是跨域,如何解决跨域呢?先看浏览器端出现跨域问题的现...

  • 跨域问题

    概述 在浏览器端进行 Ajax 请求时会出现跨域问题,那么什么是跨域,如何解决跨域呢?先看浏览器端出现跨域问题的现...

网友评论

      本文标题:新版本chrome浏览器带来的跨域请求cookie丢失问题

      本文链接:https://www.haomeiwen.com/subject/dlyiihtx.html