JWT Token 生成與token的解析



from itsdangerous import JSONWebSignatureSerializer
import jwt

def generate_jwt(secret,payload):
headers={
"alg": "HS256",
"typ": "JWT"
}

s=JSONWebSignatureSerializer(secret)
s=s.dumps(payload,header_fields=headers)
return s


def decode_jwt(secret,jwt_token):

try:
data=jwt.decode(jwt_token,secret,algorithms=['HS256'])
return data
except Exception as e:
raise e



if __name__ == '__main__':

secret= "xxxxxx"
payload={
"iss": "xxx",
"exp": 1669229208,
"userId": "xxxx"
}


jwt_token=generate_jwt(secret,payload)
print(decode_jwt(secret,jwt_token))



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM