如何跨域传递 cookie?

2016-05-27 21:47:49 +08:00
 JhOOOn
  1. 目前情况是这样的:我后台提供一接口(地址: 127.0.0.1 : 8000 ),前台有一网页(地址: 127.0.0.1 : 5000 )通过 ajax 调接口获取数据,可以获取到返回的数据,但是不能获取到返回的 cookie 。该怎么办?
  2. 后台是拿 django-rest-framework 写的(没想好,应该拿 django 写,直接获取前端的静态文件就好了。), 使用了 django-cors-headers 。
  3. 在前端浏览器的 response 头信息里可以看到 Set-Cookie 信息,但是就是获取不到( document.cookie ),也不能自动加载到浏览器里。
  4. 为什么需要前端保存 cookie 呢?因为有的要记录用户的输错次数,就拿 session 保存的,还有生成的一次性 code ,也是拿 session 保存的,这样后端就好自动生成一个 sessionid 保存到 cookie 里返回到前端。
  5. 目前感觉前台拿不到cookie应该是跨域的问题。
  6. 希望有懂的同学,提供一个大致思路。
11940 次点击
所在节点    Python
13 条回复
archer2ee
2016-05-27 22:00:40 +08:00
设置 Allow-Control-Allow-Credentials ,使得跨域可以带 Cookie ;同时要设置 Allow-Control-Allow-Origin ,此时不能用通配符。
huijiewei
2016-05-27 22:02:26 +08:00
关键字 withCredentials

但是也有限制,就是跨域服务端只能读不能写
jugelizi
2016-05-27 22:05:04 +08:00
你都知道跨域了 5000 端口的 cookie 怎么可能给你 8000 端口获取到?
接口的 cookie 保存了后台仍然可以获取到
shiji
2016-05-27 22:22:18 +08:00
我个人觉得不是跨域的问题, cookie 是不分端口的。

参考 RFC6265



For historical reasons, cookies contain a number of security and privacy infelicities. For example, a server can indicate that a given cookie is intended for "secure" connections, but the Secure attribute does not provide integrity in the presence of an active network attacker. Similarly, cookies for a given host are shared across all the ports on that host, even though the usual "same-origin policy" used by web browsers isolates content retrieved via different ports.

。。

8.5. Weak Confidentiality

Cookies do not provide isolation by port. If a cookie is readable by a service running on one port, the cookie is also readable by a service running on another port of the same server. If a cookie is writable by a service on one port, the cookie is also writable by a service running on another port of the same server. For this reason, servers SHOULD NOT both run mutually distrusting services on different ports of the same host and use cookies to store security sensitive information.
lizon
2016-05-27 22:53:13 +08:00
pimin
2016-05-27 23:04:35 +08:00
我理解的,你 8000 端口返回一个 cookie 给浏览器,但是现在用 js 在前台无法获取.
cookie 是可以本地改写 ,你 8000 端口返回一个包含 cookie 的 json 给前端 js,js 设置浏览器 cookie 不就好了?
wentx
2016-05-28 02:15:43 +08:00
http-only?
fuermosi777
2016-05-28 04:17:43 +08:00
我之前有过类似的问题,最后弄了一个简单的 nginx 把两个端口都反代到一个统一端口了。
chaegumi
2016-05-28 07:09:39 +08:00
同一个域名还是不同域名,同一个域名,只要把 cookie 的 domain 设置主顶域,这样不就行了,端口会不会有影响我不知道
julor
2016-05-28 07:35:34 +08:00
@fuermosi777 对,这招不错,我们也这样用
JhOOOn
2016-05-28 10:57:41 +08:00
@archer2ee
@fuermosi777
像楼上 2 位说的, 之前做了相关配置,还是不能用。 django-cors-headers 自带这种设置, nginx 也配置了。
后来 ajax 使用 jsonp 的方式,前端就可以自动保存 cookie 了,但是只支持 get 的方式。
最后决定先使用 token 的方式吧。
@chaegumi 这种方式还没试。
@wentx 好像看网上说不关这个的事
@shiji 好牛,专门找来文档。
已打赏,谢谢楼上所有的人,耐心的解答了。
JhOOOn
2016-05-28 11:00:48 +08:00
@lizon 之前看过,没当回事,后来看到 jsonp ,好用了,但目的不一样,对 js 的理解也不深,改用其他方式了,谢谢,已打赏。
JhOOOn
2016-05-30 14:29:43 +08:00
@fuermosi777 早上同时提醒我,将前端发送 ajax 里的地址的前缀信息去掉,就好用了。( http://a.com/api -> /api )

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/281782

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX