Loading

python实现生成 json web token

Python 生成 JWT(json web token) 及 解析方式


jwt原理及概念博客:https://www.freebuf.com/articles/web/180874.html

推荐博客:https://zhuanlan.zhihu.com/p/86937325

JWT 的官方文档: https://jwt.io/introduction/

python实现生成 json web token

环境: python3.7

依赖包: PyJWT==1.7.1

代码示例:

import jwt
from financial.settings import SECRET_KEY
#每一个人的SECRET_KEY都是不一样的 

# 加密
def jwt_start(data):
    encode_jwt = jwt.encode(data, SECRET_KEY, algorithm='HS256')	#加密方式HS256
    # 强转
    encode_jwt = str(encode_jwt, encoding='utf-8')
    return encode_jwt


# 解密
def jwt_end(jwt_data):
    decode_jwt = jwt.decode(jwt_data, SECRET_KEY, algorithms=['HS256']).get('token')
    return decode_jwt

posted @ 2020-12-21 20:47  就学45分钟  阅读(186)  评论(0编辑  收藏  举报