参考网址:
1.https://www.cnblogs.com/chnmig/p/10143324.html 2.https://www.cnblogs.com/viplanyue/p/12700607.html 3.flask demo https://www.cnblogs.com/xujunkai/p/12360077.html
1.下载模块
pip install pyjwt==2.0.1
2.代码实例
import jwt from jwt import exceptions import datetime import time #1.头部 headers = { "typ": "JWT", "alg": "HS256", } #2.载荷 playod = { # 'exp': datetime.datetime.now() + datetime.timedelta(seconds=3), # 过期时间 'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=3), # 过期时间 # 'iat': datetime.datetime.now(), # 开始时间:可不设置 'iat': datetime.datetime.utcnow(), # 开始时间:可不设置 'iss': 'lianzong', # 签名:可不设置 'data': { # 内容,一般存放该用户id和开始时间 'username': "张三", 'age': 18, }, } #3.签发token key = "dbvjfbvbvfnbflsd" #加密解密密钥,可随意定义 token_data = jwt.encode(payload=playod, key=key, algorithm='HS256') print(token_data) #4.校验token time.sleep(5) try: #issuer='lianzong',要与在和中的相对应 pasre = jwt.decode(token_data, key, issuer='lianzong', algorithms=['HS256']) # 解密,校验签名 except exceptions.ExpiredSignatureError: print("token已失效") except jwt.DecodeError: print("token认证失败") except jwt.InvalidTokenError: print("非法的token") else: print("正确解析",pasre)