python的生成Jwt

import jwt
import datetime

#载荷中加入生命周期的概念
playload={
    #过期时间,设置120s 过期
    'exp':int((datetime.datetime.now()+datetime.timedelta(seconds=120)).timestamp()),
    #给id为2的用户添加密钥
    'data':{'uid':2}
}

#生成jwt,编码,‘qwe123’是密钥,自己定义的
encode_jwt = jwt.encode(playload,'qwe123',algorithm='HS256')

#得到的结果是一个字节码,可以转码成字符串
# print(encode_jwt)

# #转码
encode_str = str(encode_jwt,'utf-8')
print(encode_str)

#解密  jwttoken:encode_str,  密钥:'qwe123', 算法:algorithms
decode_jwt = jwt.decode(encode_str,'qwe123',algorithms=['HS256'])
print(decode_jwt)

ps:有不懂python时间戳的,可参考我的一篇随笔 python时间板块,计算取值,math函数

posted @ 2020-06-06 07:50  Bronya天使  阅读(467)  评论(0编辑  收藏  举报