• 【12】python模块:itsdangerous(生成临时身份令牌)


    使用itsdangerous生成临时身份令牌

    安装

    pip install itsdangerous

    使用

    import itsdangerous
    
    salt='sdaf'#加盐
    t=itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=600)#过期时间600秒
    
    info = {'username':'yangfan','user_id':1}
    
    # =========加密token============
    res=t.dumps(info)
    token=res.decode()#指定编码格式
    print(token)
    # eyJleHAiOjE1NzUwMDczNjgsImlhdCI6MTU3NTAwNjc2OCwiYWxnIjoiSFM1MTIifQ.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InlhbmdmYW4ifQ.yUb3PW53V89ZX4Ci2qeaBJIiizt0JUAN_W9BBzg8QkIR1-uO7NQl6jizSUReOFGanWzfG19t7XFHCWv1JGMIZw
    
    
    # =========解密token============
    
    res = t.loads('eyJhbGciOiJIUzUxMiIsImV4cCI6MTU3NTAwNzM0MywiaWF0IjoxNTc1MDA2NzQzfQ.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InlhbmdmYW4ifQ.k-Q1VyN2TOlQ4flHHoiOYEMRaUEiN5Ms2JgeRdnCZWbQB-WwQ1FScoBWxFGkCYEPoWVpAjQxDBQeBesmulZupQ')
    # res = t.loads(token)
    print(res)
    # {'username': 'yangfan', 'user_id': 1}
    
    
    # 当超时或值有误则会报错
    import itsdangerous
    
    salt='sdaf'#加盐
    t=itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=600)#过期时间600秒
    
    info = {'username':'yangfan','user_id':1}
    
    # =========加密token============
    res=t.dumps(info)
    token=res.decode()#指定编码格式
    print(token)
    # eyJhbGciOiJIUzUxMiIsImlhdCI6MTU0MTgxOTcyMCwiZXhwIjoxNTQxODIwMzIwfQ.eyJ1c2VybmFtZSI6InlhbmdmYW4iLCJ1c2VyX2lkIjoxfQ.VjCgry9Sr-4iRsK_MHYThcn_O7js9BERrXzocc7BI1aavC3N3s3e0wWMsvq2-Qp-ol_WNMD23wxiYRrA1kwCbg
    
    # =========解密token============
    
    res = t.loads(token)
    print(res)
    # {'username': 'yangfan', 'user_id': 1}
    
    
    # 当超时或值有误则会报错
  • 相关阅读:
    读书笔记五
    读书笔记四
    读书笔记3(Teamwork)
    读书笔记二(合格的软件工程师)
    读书笔记1(软件 = 程序 + 工程)
    关于使用Java开发Mis系统
    课堂动手动脑
    Quartz学习
    把数据库中取出的DataTable转换成一个对象 或者对象列表
    SAE上使用cron定时发微博
  • 原文地址:https://www.cnblogs.com/haifeima/p/9963103.html
Copyright © 2020-2023  润新知