Cookie的跨域共享

来源:互联网 发布:千金方医药软件 编辑:程序博客网 时间:2024/06/05 05:02

这两天遇到了域名切换的问题,因为需要域名的兼容,尝试创建多份cookie,将cookie种在不同的域上,尝试了下,单纯从后端进行改动是没办法实现的。
查阅相关资料,了解了cookie的domain限制,看RFC-6265中对domain match的介绍

A string domain-matches a given domain string if at least one of the   following conditions hold:   o  The domain string and the string are identical.  (Note that both the domain string and the string will have been canonicalized to lower case at this point.)   o  All of the following conditions hold:      *  The domain string is a suffix of the string.      *  The last character of the string that is not included in the domain string is a %x2E (".") character.      *  The string is a host name (i.e., not an IP address).

这里对cookie domain的就是浏览器在向request中写入cookie以及从response中向浏览器中存储cookie时,进行的限制。就是cookie的域当前url域名的关系,必须满足下面两个条件之一才行:

  1. cookie domain和url完全相同(经过转小写处理后)

  2. 必须满足下面的所有条件
    2.1 cookie域是url的后缀
    2.2 url去掉cookie domain部分后,最后一个字符必须是 . (dot)
    2.3 url是域名,不是IP地址。

因此纯粹从后端实现cookie的跨域共享不太可能(能实现的话,这就是一个超级大的安全漏洞了),可以通过前端+后端的方式共同实现,eg:

原创粉丝点击