跨域setCookie总结
本文由 小茗同学 发表于 2025-04-27 浏览(29)
最后修改 2025-04-27 标签:

http调用http

服务端:

this.ctx.response.set('set-cookie', 'test-cookie1=111; path=/;');

只需要满足跨域、请求带上credentials: 'include'就能正常写入cookie(没有感叹号就表示成功):

https调用https

同上,服务端:

this.ctx.response.set('set-cookie', 'test-cookie1=111; path=/;');

只需要满足跨域、请求带上credentials: 'include'就能正常写入cookie。

http调用http

如果还是上述服务端代码,写入会不成功,提示要指定sameSite

修改后服务端:

this.ctx.response.set('set-cookie', 'test-cookie2=222; path=/; samesite=None; secure;');

写入成功:

总结

  • protocol相同,无需主动指定sameSite,只要保证跨域没问题就可以写入cookie成功;
  • protocol不相同需要主动设置sameSite+Secure;