目录

H5网页中Cookie跨域场景及解决方案

问题现象

H5网页中有哪些Cookie跨域场景?针对这些场景,又如何解决?

背景知识

解决方案

Web组件加载网页时,主要存在子域名跨域、第三方Cookie跨域2种场景。

常见FAQ

Q:Cookie设置Secure属性时,通过官网指导使用DevTools工具调试前端页面时,Application-Cookie内无法显示Cookie?

A:设置Secure属性时,若是http协议DevTools不会显示Cookie,改成https协议访问即可。

Q:WebCookieManager的putAcceptThirdPartyCookieEnabled和putAcceptCookieEnabled有什么区别?

A:putAcceptThirdPartyCookieEnabled控制是否发送与接收第三方Cookie,而putAcceptCookieEnabled控制是否发送与接收同域Cookie,浏览器通常会默认禁止第三方Cookie,而允许同域Cookie。

因此,需要使用三方Cookie则将putAcceptThirdPartyCookieEnabled设置为true,需要使用同域Cookie则将putAcceptCookieEnabled设置为true。

Q:WebCookieManager的putAcceptThirdPartyCookieEnabled和putAcceptCookieEnabled都设置为true,有什么安全风险?

A:如果设置两者为true,意味着浏览器或应用将接受第三方Cookie和所有Cookie。这种设置可能会增加以下安全风险:

Q:跨域报错The value of the "Access Control-Allow-Origin" header in the response must not be the wildcard '*' when the request's credentials mode is "include"是什么原因?

A:Access-Control-Allow-Origin不能为通配符“*”,必须指定具体的前端源,确保权限可控。

Q:configCookieSync设置token=123; Domain=www.example.com后,使用DevTools工具调试前端页面时,查看设置的token Cookie,Domain显示为.www.example.com?

A:设置Domain时会在域名前加上(.)是chromium默认行为,是符合预期的。