V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
bmcjxhgwy
V2EX  ›  Python

新人想问个 cookie 和 session 的事

  •  
  •   bmcjxhgwy · Oct 2, 2017 · 4327 views
  •   You need to sign in to view this topic
    This topic created in 3137 days ago, the information mentioned may be changed or developed.

    session 是存储在服务器所以比 cookie 安全,但是为什么不能通过获取存储在客户端的 session_id 然后像服务器发起请求呢?

    9 replies    2017-10-03 15:45:20 +08:00
    disk
        1
    disk  
       Oct 2, 2017 via Android
    cookie 也是保存 session id 的一种方式。我想你可能有些地方没搞明白。。。
    Sornets
        2
    Sornets  
       Oct 2, 2017
    能。
    lululau
        3
    lululau  
       Oct 2, 2017
    1. session 数据不只可以存储在服务器端,也可以存储在客户端,比如 Rails 默认的 Session 存储方案就是存在一个加密的 Cookie 里的
    2. 可以
    guoyang
        4
    guoyang  
       Oct 2, 2017   ❤️ 1
    由于 http 是无状态的,所以 session 的 ID 可以存在 cookie 中,也可以存在 LocalStorage 活着 SessionStorage,甚至是页面的隐藏字段。所谓的不安全分为两种,一是逻辑不安全,如果我们拿 cookie 中的数据直接作为我们逻辑的一部分,这部分数据是极容易被篡改的,所以我们通过 SessonId,拿服务端的数据作为逻辑的数据;二是 cookie 容易被 XSS / CSRF 攻击,虽然有同源策略的保护,现在较流行的方式是用 Token ( JWT )来取代 cookie 的设计。
    wentaoliu
        5
    wentaoliu  
       Oct 2, 2017
    @guoyang 如果浏览器中使用 jwt 仍然需要将 token 放到 cookie 或者 localstorage 中,还是可能会被 XSS 或 CSRF
    wwhc
        6
    wwhc  
       Oct 3, 2017
    只用 cookie 就够了, 有 cookie 还用 session 是多此一举
    guoyang
        7
    guoyang  
       Oct 3, 2017 via iPhone
    @wentaoliu 所以 token 的设计中可以加 ip 加 useragent 或者尽量能够标示出 client 端的东西,跨站攻击本来就是泄漏,这个只能从源头控制。防护可以分两种,一是防用户本身,Xss 或者中间人攻击防的是第三者,token 防用户自身能够做的很好。token 本身的出发点并不是解决所有的安全问题
    tuzhenyu
        8
    tuzhenyu  
       Oct 3, 2017
    cookie 中的 sessionId 过期机制就是用来减弱 sessionId 被盗带来的影响
    opengps
        9
    opengps  
       Oct 3, 2017
    cookie 中的 sessionId 默认是禁止读取的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   763 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 20:36 · PVG 04:36 · LAX 13:36 · JFK 16:36
    ♥ Do have faith in what you're doing.