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后的效果:

  • 相关阅读:
    字符串阵列分别输出元素的索引,原值和长度
    一个字符转换为整数
    从字符串数组中寻找数字的元素
    C#创建自己的扩展方法
    判断是否为空然后赋值
    C# yield关键词使用
    从字符串数组中把数字的元素找出来
    C#实现一张塔松叶
    计算2个时间之间经过多少Ticks
    对int array进行排序
  • 原文地址:https://www.cnblogs.com/wangcongxing/p/12340228.html
Copyright © 2011-2022 走看看