自定义JWT加密(这是JWT,不是drf中的jwt)
安装
pip install PyJwT # 我是# 1.7.1 版本
import jwt
# 1.加密
encode_jwt=jwt.encode({'uid':'123','name':'xxx'},'666',algorithm='HS256') # 加密内容,密钥,算法
print(encode_jwt)
# result》》 b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxMjMiLCJuYW1lIjoieHh4In0.2c764nB6R2peV_BYLW4uUo-78EOWoPdaaER28R-31Tw'
# 2.解密
# 强转
encode_jwt=str(encode_jwt,'utf-8') # 强转后 没有b
# result》》'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxMjMiLCJuYW1lIjoieHh4In0.2c764nB6R2peV_BYLW4uUo-78EOWoPdaaER28R-31Tw'
decode_jwt=jwt.decode('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxMjMiLCJuYW1lIjoiXHU1ZjIwXHU3ZmZjXHU2MjZjIn0.ulCbmhsXdZHKpWWEz-E8oPR9R2H19JRR8mF2FNPHFZo',
'666',algorithms=['HS256']) # 加密内容,密钥,算法
print(decode_jwt)
# result》》 {'uid': '123', 'name': 'xxx'}