- 前端请求时在request对象中配置"withCredentials": true;
- 服务端在response的header中配置"Access-Control-Allow-Origin", "http://xxx:${port}";
- 服务端在response的header中配置"Access-Control-Allow-Credentials", "true"
除此之外还有一个条件便是https,因为现在最新版得情况是这个配置是带不了cookie得。现在cookie得samesite属性默认是lax,lax模式在很多情况都是不允许跨域携带cookie。必须要把samesite设置为none,但是设置为none有一个要求,就是必须secure属性为true,也就是必须使用https。
也就是还需要:
1.网站开启 https 并将 Cookie 的 Secure 属性设置为 true
2.SameSite 属性设置为 None
注:samesite 的作用顾名思义同一站点 ,Secure属性是说如果一个cookie被设置了Secure=true,那么这个cookie只能用https协议发送给服务器,用http协议是不发送的。换句话说,cookie是在https的情况下创建的,而且他的Secure=true,那么之后你一直用https访问其他的页面
其余知识点:设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie打出cookie的内容。
https://www.cnblogs.com/forwill/p/6181984.html
https://juejin.cn/post/6963632513914765320
https://juejin.cn/post/7066420545327218725