Cookie的跨域共享

这两天遇到了域名切换的问题,因为需要域名的兼容,尝试创建多份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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值