HTTP服务设置Cookie失败
最近在开发中将原本的HTTPS服务器同时也开放HTTP服务,Chrome发现
Set-Cookie
失败了;但是在历史版本中是可以的,这里分析记录下。
问题现象
设置的Cookie
指定参数:Path=/;Httponly;Secure;
。
报文一切正常,但是F12查看时可以看到Cookie有异常提示:This Set-Cookie was blocked because it had the "Secure" attribute but was not received over a secure connection. This Set-Cookie was blocked because it was not sent over a secure-connection and would have overwritten a cookie with Secure attribute.
问题原因
第一个原因就比较明确了,HTTP服务不支持Secure
安全属性。Secure
表示Cookie
会以安全的形式传输,HTTP肯定是不满足的。
实际测试发现即使去掉了Secure
属性依旧不可以,这是因为此前曾访问过它的HTTPS服务,而HTTPS服务的Cookie是有设置过Secure
属性,在Chrome中将HTTPS页面的Cookie等缓存清空即可。
如果期望同时支持HTTPS和HTTP服务器,那么HTTPS/HTTP都需要放弃Secure
属性才可以,因为不能要求使用者清楚缓存数据。
本质是因为Cookie在不同端口之间是共享的
网友评论