pip3 install itsdangerous
安装import itsdangerous
import itsdangerous
from itsdangerous import SignatureExpired, BadSignature
# 设置盐
salt = '2019.5.6'
# 设置过期时间
ts = itsdangerous.TimedJSONWebSignatureSerializer(salt, expires_in=30)
# 加密部分,通过盐和数据部分生成token值
token = ts.dumps({'salt': 'salt'}).decode()
# eyJhbGciOiJIUzUxMiIsImlhdCI6MTU1NzEyMzIzNywiZXhwIjoxNTU3MTIzMjY3fQ.eyJzYWx0Ijoic2FsdCJ9.t42s-UoTr97qwzMuxPBDbD3uC02Mm53p51WbCybN5RnEI3FN1NgKuY-4RBJAtnXXPajD81YMhg79uOaaYpwPMg
# 解密部分/如果超过过期时间则会报SignatureExpired错误/值有误会报BadSignature错误
try:
ret = ts.loads(token)
#{'salt': 'salt'}
except SignatureExpired as e:
print('超时')
except BadSignature as e:
print('token有误')