V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
JhOOOn
V2EX  ›  Python

如何跨域传递 cookie?

  •  
  •   JhOOOn · 2016-05-27 21:47:49 +08:00 · 11929 次点击
    这是一个创建于 2883 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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. 希望有懂的同学,提供一个大致思路。
    第 1 条附言  ·  2016-05-30 14:33:03 +08:00
    可以实现,参看这里: http://www.jianshu.com/p/62ae04e16dce
    13 条回复    2016-05-30 14:29:43 +08:00
    archer2ee
        1
    archer2ee  
       2016-05-27 22:00:40 +08:00 via iPhone   ❤️ 1
    设置 Allow-Control-Allow-Credentials ,使得跨域可以带 Cookie ;同时要设置 Allow-Control-Allow-Origin ,此时不能用通配符。
    huijiewei
        2
    huijiewei  
       2016-05-27 22:02:26 +08:00
    关键字 withCredentials

    但是也有限制,就是跨域服务端只能读不能写
    jugelizi
        3
    jugelizi  
       2016-05-27 22:05:04 +08:00
    你都知道跨域了 5000 端口的 cookie 怎么可能给你 8000 端口获取到?
    接口的 cookie 保存了后台仍然可以获取到
    shiji
        4
    shiji  
       2016-05-27 22:22:18 +08:00 via Android   ❤️ 1
    我个人觉得不是跨域的问题, 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
        5
    lizon  
       2016-05-27 22:53:13 +08:00   ❤️ 1
    pimin
        6
    pimin  
       2016-05-27 23:04:35 +08:00
    我理解的,你 8000 端口返回一个 cookie 给浏览器,但是现在用 js 在前台无法获取.
    cookie 是可以本地改写 ,你 8000 端口返回一个包含 cookie 的 json 给前端 js,js 设置浏览器 cookie 不就好了?
    wentx
        7
    wentx  
       2016-05-28 02:15:43 +08:00   ❤️ 1
    http-only?
    fuermosi777
        8
    fuermosi777  
       2016-05-28 04:17:43 +08:00   ❤️ 1
    我之前有过类似的问题,最后弄了一个简单的 nginx 把两个端口都反代到一个统一端口了。
    chaegumi
        9
    chaegumi  
       2016-05-28 07:09:39 +08:00   ❤️ 1
    同一个域名还是不同域名,同一个域名,只要把 cookie 的 domain 设置主顶域,这样不就行了,端口会不会有影响我不知道
    julor
        10
    julor  
       2016-05-28 07:35:34 +08:00 via Android
    @fuermosi777 对,这招不错,我们也这样用
    JhOOOn
        11
    JhOOOn  
    OP
       2016-05-28 10:57:41 +08:00
    @archer2ee
    @fuermosi777
    像楼上 2 位说的, 之前做了相关配置,还是不能用。 django-cors-headers 自带这种设置, nginx 也配置了。
    后来 ajax 使用 jsonp 的方式,前端就可以自动保存 cookie 了,但是只支持 get 的方式。
    最后决定先使用 token 的方式吧。
    @chaegumi 这种方式还没试。
    @wentx 好像看网上说不关这个的事
    @shiji 好牛,专门找来文档。
    已打赏,谢谢楼上所有的人,耐心的解答了。
    JhOOOn
        12
    JhOOOn  
    OP
       2016-05-28 11:00:48 +08:00
    @lizon 之前看过,没当回事,后来看到 jsonp ,好用了,但目的不一样,对 js 的理解也不深,改用其他方式了,谢谢,已打赏。
    JhOOOn
        13
    JhOOOn  
    OP
       2016-05-30 14:29:43 +08:00
    @fuermosi777 早上同时提醒我,将前端发送 ajax 里的地址的前缀信息去掉,就好用了。( http://a.com/api -> /api )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5696 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:08 · PVG 10:08 · LAX 19:08 · JFK 22:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.