zoukankan      html  css  js  c++  java
  • Django学习小记-cookie的使用(判断是否已登陆)

    有没有发现我们即使做了登陆框login界面,但别人还是可以通过知道URL就可以访问站点!

    这是因为缺少cookie

    def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
                       domain=None, secure=False, httponly=False, samesite=None):
            """
            Set a cookie.
    
            ``expires`` can be:
            - a string in the correct format,
            - a naive ``datetime.datetime`` object in UTC,
            - an aware ``datetime.datetime`` object in any time zone.
            If it is a ``datetime.datetime`` object then calculate ``max_age``.
            """
    #max_age是多少秒后失效
    #expire是间隔多久后失效,同max_age;建议用max_age
    #path='/' 默认所有下url共用该cookie ,也可指定只有特定url使用cookie
    #domain是一二级域名是否可共用cookie
    #secure与https有关,若用https则改值设置为True
    #httponly安全相关,只有http来回发请求的时候才能用,通过js代码无法获取到。
    View Code

    我们修改login和index函数添加cookie验证:

    #登陆
    def login(request):
         if request.method == "GET":
            return render(request,'login.html')
        else:
            #用户POST提交的数据
            user = request.POST.get('user')
            pwd = request.POST.get('pwd')
            if user == 'admin' and pwd =='123':
                obj = redirect('/index/')
                obj.set_cookie('ticket','asasasasasaas',max_age=3600)
                return obj
            else:
                return render(request,'login.html')
    
    #主页
    def index(request):
        #去请求的cookie中找凭证;有明文的和带签名的两种方式,下面是不带签名的
        tk = request.COOKIES.get('ticket')
        if not tk:
            return redirect('/login/')
    
        else:
            #去数据库获取数据
            secfile_list = sqlheper.get_list("select id,CaseName,CaseType,Level,Cause,Result from anfu",[])
            return render(request,'index.html',{'secfile_list':secfile_list})

     再解释一下带签名cookie的如何做验证:

    #登陆
    def login(request):
         if request.method == "GET":
            return render(request,'login.html')
        else:
            #用户POST提交的数据
            user = request.POST.get('user')
            pwd = request.POST.get('pwd')
            if user == 'admin' and pwd =='123':
                obj = redirect('/index/')
           #下面这个就是带签名的cookie
                obj.set_signed_cookie('ticket','asasasasasaas',salt='shannon')
                return obj
            else:
                return render(request,'login.html')
    
    #主页
    def index(request):
        #去请求的cookie中找凭证;有明文的和带签名的两种方式
        tk = request.get_signed_cookie('ticket',default="0",salt='shannon')
        if tk != "asasasasasaas":
            return redirect('/login/')
    
        else:
            #去数据库获取数据
            secfile_list = sqlheper.get_list("select id,CaseName,CaseType,Level,Cause,Result from anfu",[])
            return render(request,'index.html',{'secfile_list':secfile_list})
    View Code

    需要了解:

    signed_cookie 只是加了签名的 cookie, 而不是被加密的 cookie.在客户端还是可以看到没有加密的value的

    signed_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, 但是没有签名, 服务端照样拒绝访问资源.
    ————————————————
    原文链接:https://blog.csdn.net/weixin_43976393/article/details/88959586

    -还可以自定义cookie签名

    -可用装饰器装饰views中的函数

    #装饰器cookie
    def cook(func):
        def inner(request):
            tk = request.get_signed_cookie('ticket', default="0", salt='shannon')
            if tk != "asasasasasaas":
                return redirect('/login/')
            else:
                return func(request)
        return inner
    
    @cook
    def add_class(request):
    ...
    View Code
    为美好的生活奋斗!
  • 相关阅读:
    angularJS 修改操作select回显选中的数据
    zkteco iface702 中控考勤机java开发步骤二---获取考勤机 的考勤数据
    zkteco iface702 中控考勤机java开发步骤一---连接考勤机
    JQuery的分页插件pagination.js
    Ajax跨域后台处理
    发送邮箱工具类--阿里企业邮箱群发
    kindeditor-4.1.10 ---文件上传
    导出excel表格
    算法
    Arrays类——Arrays.asList()方法使用
  • 原文地址:https://www.cnblogs.com/ethtool/p/12167588.html
Copyright © 2011-2022 走看看