zoukankan      html  css  js  c++  java
  • django set_signed_cookie 方法理解

    djang set_signed_cookie 理解

    signed_cookie 只是加了签名的 cookie, 而不是被加密的 cookie.

    signed_cookie 的作用是防止用户私自纂改.参考: Securing Web Cookies With Signatures

    So once I’ve logged in, we set a username cookie containing “Michael Brunton-Spall”, or uid=1 or something.
    The problem with this is that the user is in total control of this cookie
    单纯的记录 uid 或者用户名在 cookie 中很容易被篡改(也是不建议将用户敏感信息记录在cookie中的原因), 万一攻击者把uid=1换成uid=2岂不是可以访问 uid=2用户的资源了吗? 而如果换成uid=2:1fPjh2:iQGDDYNcgSYkIFqa2ixqakj6-gI那么服务端不仅检验uid, 还检验uid=2后面的签名字段, 即是调用HttpRequest.get_signed_cookie(key=key, salt=salt), 这样即使用户把 cookie 中的 value 换成 uid=2, 但是没有签名, 服务端照样拒绝访问资源.

    另外, Django 的 cookie 签名是用的Base64_with_hmac, 参考: Source code for django.core.signing

    如果需要在 cookie 里设置被加密的 value, 需要自行对 value 进行加密(好像只能是对称加密), 比如使用hashlib.sha256{参考: hashlib — Secure hashes and message digests}:

    cookie签名代码:

    response.set_signed_cookie("qyUserName", "WANGCONGXING", salt="d8s#wc%0t7")

    设置cookie后的效果:

  • 相关阅读:
    nginx原理及常用配置
    课程作业03-1
    Java动手动脑02
    Java课程作业02
    java课堂测试2
    Java验证码程序
    课程作业02-2
    课程作业02-1
    课程作业01
    《大道至简》第一章伪代码
  • 原文地址:https://www.cnblogs.com/wangcongxing/p/12340228.html
Copyright © 2011-2022 走看看