首页 > 其他分享 >JWT 的使用方式

JWT 的使用方式

时间:2022-10-29 19:00:22浏览次数:67  
标签:方式 JWT jwt decode token 使用 encoded payload

 

django中使用JWT

 https://blog.csdn.net/wolflxiaolu/article/details/124381344  

中间件进行jwt校验 middlewares/jwt.py

restframework与pyJWT结合

pip install pyJWT

  1. from utils.jwt_auth import parse_payload
  2.  
  1. from utils.jwt_auth import create_token
  2.  

在线JWT Token解析解码

https://tooltt.com/jwt-decode/

JWT认证方式了解

 

【Python】jwt解码

    https://blog.csdn.net/qq_44647926/article/details/127191843  

使用Python生成和解码JWTToken

 https://blog.csdn.net/weixin_44786530/article/details/123210467  

import base64

import jwt
from datetime import datetime, timedelta


def encode_token():
payload = {
'exp': datetime.now() + timedelta(minutes=30), # 令牌过期时间
'username': 'BigFish' # 想要传递的信息,如用户名ID
}
key = """{"kty":"RSA","kid":"5995191754202518511","alg":"RS256","n":"lSbCWJdg-73rjKfV5ZijV0dyxZXnH-Ev3eVgkkZbab9xelhTlExdnBKDD1d6F9C_ZkvyV3tOg1tcD27iKZ4WD2Xy6vvUvNj6fxtbcgKOQnW2G5l2aJ69EegXE6RG9yc6EjKX69361t8pZ76MWFQ3BNt-X-6ZqQRJgrzDGD5pTMXHaZceuIIA3blVtBIylKmowt2ug6ieKTMk6CEzdDetBDSeGnXc8vgNQa3TzSvYW4wRVmCIYVby_qKUQqqAnWw1RtrtnLNrp0cJHiCf05g7eRttc4xHA0u4GnhlJEuEVQSr_7DC3tEMcrdH30l_PqO6FHs8DoEn6LrFCsG1oSza2w","e":"AQAB"}"""
encoded_jwt = jwt.encode(payload, key, algorithm='HS256')
print(encoded_jwt)
return encoded_jwt


def decode_token(encoded_jwt):
key = """{"kty":"RSA","kid":"5995191754202518511","alg":"RS256","n":"lSbCWJdg-73rjKfV5ZijV0dyxZXnH-Ev3eVgkkZbab9xelhTlExdnBKDD1d6F9C_ZkvyV3tOg1tcD27iKZ4WD2Xy6vvUvNj6fxtbcgKOQnW2G5l2aJ69EegXE6RG9yc6EjKX69361t8pZ76MWFQ3BNt-X-6ZqQRJgrzDGD5pTMXHaZceuIIA3blVtBIylKmowt2ug6ieKTMk6CEzdDetBDSeGnXc8vgNQa3TzSvYW4wRVmCIYVby_qKUQqqAnWw1RtrtnLNrp0cJHiCf05g7eRttc4xHA0u4GnhlJEuEVQSr_7DC3tEMcrdH30l_PqO6FHs8DoEn6LrFCsG1oSza2w","e":"AQAB"}"""
res = jwt.decode(encoded_jwt, key, algorithms='HS256', options={"verify_signature": False})
print(res)


def test_decode():
header = base64.b64decode('eyJhbGciOiJSUzI1NiIsImtpZCI6IjU5OTUxOTE3NTQyMDI1MTg1MTEifQ')
payload = base64.b64decode(
'eyJlbWFpbCI6IjEzMjM0MjFAcXEuY29tIiwibmFtZSI6Im1haWNlIiwibW9iaWxlIjpudWxsLCJleHRlcm5hbElkIjoiODk2OTM3MDYzMjU0MTE2MjEzIiwidWRBY2NvdW50VXVpZCI6ImQ4Nzc1NDBhN2E3N2Y0NzI5NmVjZmY5OTA3OTQzNzQ0U2tOYnhVTEptNXkiLCJvdUlkIjoiMjMyMDE4OTI4MDc2MDk5MzY1MSIsIm91TmFtZSI6IumVv-S4ieinkiIsIm9wZW5JZCI6bnVsbCwiaWRwVXNlcm5hbWUiOiJtYWljZSIsInVzZXJuYW1lIjoibWFpY2UiLCJhcHBsaWNhdGlvbk5hbWUiOiLmmbrog73pgInlnYBf6KeE5YiS566h55CGIiwiZW50ZXJwcmlzZUlkIjoiaWRhYXMiLCJpbnN0YW5jZUlkIjoiaWRhYXMiLCJhbGl5dW5Eb21haW4iOiIiLCJleHRlbmRGaWVsZHMiOnsidGhlbWVDb2xvciI6ImdyZWVuIiwiYXBwTmFtZSI6IuaZuuiDvemAieWdgF_op4TliJLnrqHnkIYifSwiZXhwIjoxNjQ2MTI0Mjc2LCJqdGkiOiIwZ3RmV3pxTHVvckc1M09jVUtZTkhBIiwiaWF0IjoxNjQ2MTIzNjc2LCJuYmYiOjE2NDYxMjM2MTYsInN1YiI6Im1haWNlIiwiaXNzIjoiaHR0cDovLzEwLjkwLjUuOTAvIiwiYXVkIjoiaWRhYXNwbHVnaW5fand0MTQifQ')
# unsignedToken = encodeBase64(header) + '.' + encodeBase64(payload)
# signature = HMAC - SHA256(key, unsignedToken)
print(header)
print(payload)


if __name__ == '__main__':
encoded_jwt = encode_token()
encoded_jwt = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjU5OTUxOTE3NTQyMDI1MTg1MTEifQ.eyJlbWFpbCI6IjEzMjM0MjFAcXEuY29tIiwibmFtZSI6Im1haWNlIiwibW9iaWxlIjpudWxsLCJleHRlcm5hbElkIjoiODk2OTM3MDYzMjU0MTE2MjEzIiwidWRBY2NvdW50VXVpZCI6ImQ4Nzc1NDBhN2E3N2Y0NzI5NmVjZmY5OTA3OTQzNzQ0U2tOYnhVTEptNXkiLCJvdUlkIjoiMjMyMDE4OTI4MDc2MDk5MzY1MSIsIm91TmFtZSI6IumVv-S4ieinkiIsIm9wZW5JZCI6bnVsbCwiaWRwVXNlcm5hbWUiOiJtYWljZSIsInVzZXJuYW1lIjoibWFpY2UiLCJhcHBsaWNhdGlvbk5hbWUiOiLmmbrog73pgInlnYBf6KeE5YiS566h55CGIiwiZW50ZXJwcmlzZUlkIjoiaWRhYXMiLCJpbnN0YW5jZUlkIjoiaWRhYXMiLCJhbGl5dW5Eb21haW4iOiIiLCJleHRlbmRGaWVsZHMiOnsidGhlbWVDb2xvciI6ImdyZWVuIiwiYXBwTmFtZSI6IuaZuuiDvemAieWdgF_op4TliJLnrqHnkIYifSwiZXhwIjoxNjQ2MTI0Mjc2LCJqdGkiOiIwZ3RmV3pxTHVvckc1M09jVUtZTkhBIiwiaWF0IjoxNjQ2MTIzNjc2LCJuYmYiOjE2NDYxMjM2MTYsInN1YiI6Im1haWNlIiwiaXNzIjoiaHR0cDovLzEwLjkwLjUuOTAvIiwiYXVkIjoiaWRhYXNwbHVnaW5fand0MTQifQ.CddyMl_q9eK1Mfid-gYilJ43D-wMcyNW3GliTZh6JrFKFn8pHsVwnsYxDFlk93vcp7tK3_HyGCSV99AfF8LKj9q1VE3B83q9fcTK8pyc67EJJDonRRuYXbx82_34V5N3UhZBHAxhrIwmO943GeHuobBumXSfTtKhiX-VJ1taPQDMJvMDGbe-wCHJws18NZypjFYccoZjvc-Biy8z7y-ST0dit_TiwGDSNvDd5I1hSxGtcfUtkUIRuaO6xRZ6KC_PTvD_J5-jmHOjpLjp99rIHpS3FLhHmDKHPzDDiAgHUrel1hp6ZMmldWA27HjuUNZs8gVngZJXJ92sKY3d0btRDQ"
decode_token(encoded_jwt)
# test_decode()

标签:方式,JWT,jwt,decode,token,使用,encoded,payload
From: https://www.cnblogs.com/hongdoudou/p/16839410.html

相关文章

  • Go接口嵌套的使用
    原文链接:https://www.zhoubotong.site/post/86.html这里介绍下接口interface嵌套的用法,大家知道Go语言中不仅仅结构体与结构体之间可以嵌套,接口与接口之间也可以嵌套,通过......
  • 使用JMeter的条件--安装Java
    首先下载JMeter,网址:https://jmeter.apache.org/dow...下载Binaries版,即可执行版,已编译好,下载解压就可以用。Source版是源代码版,需要自己编译成可执行文件。tgz压缩格式......
  • Python 自由方法使用说明
     #自由方法只能通过类对象来调用,如果用实例对象调用,传入的第一个参数就是实例对象本身classtest(object):old="类变量,外部可访问。"#自由方法def......
  • 关于python中的assert断言函数的使用与说明
    assert函数是一个简单的测试工具,如果assert后面的语句,或者表达式结果,是False(或者0,空,None)的话,这个程序就会异常退出举例说明,如下创建一py脚本,内容如下:importsysasser......
  • 3.3 使用Python打开Excel文件_获取一系列单元格的内容
    3.获取一系列格式sheet[‘A1:A5’]   sheet[‘A’]     sheet[‘A:C’]    sheet[5]4.指定行和列的范围,按行获取,按列获取.iter_rows(min_row=最低行数,max......
  • conda命令使用
    主要参考Anaconda官方指南UsingConda:https://conda.io/docs/using/index.html环境:Win1064bitwith conda 4.3.14以下命令均在windows命令行中输入。一般来讲,无论是在L......
  • Git 02: git管理码云代码仓库 + IDEA集成使用git
    Git项目搭建创建工作目录与常用指令工作目录(WorkSpace)一般就是你希望Git帮助你管理的文件夹,可以是你项目的目录,也可以是一个空目录,建议不要有中文。日常使用只要记住......
  • TypeScript日期工具: date-fns日期工具的使用方法
    1、引入$npminstall--savedate-fns2、使用import{isToday,isYesterday,isTomorrow,format,addYears,addMonths,addDays,addHours,add......
  • Istio(五):使用服务网格Istio进行流量路由
    目录一.模块概览二.系统环境三.简单路由3.1简单路由四.Subset和DestinationRule4.1Subset和DestinationRule4.2DestinationRule中的流量策略4.2.1负载均衡器设置4.2......
  • Demo51_关于构造器有参构造的使用
    //构造器:有参构造的使用packagecom.oop.demo2;publicclassperson_3_2{Stringname;intage;publicperson_3_2(){}publicperson_3_2(Stringna......