使用itsdangerous生成临时身份令牌
安装
pip install itsdangerous
使用
import itsdangerous salt='sdaf'#加盐 t=itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=600)#过期时间600秒 info = {'username':'yangfan','user_id':1} # =========加密token============ res=t.dumps(info) token=res.decode()#指定编码格式 print(token) # eyJleHAiOjE1NzUwMDczNjgsImlhdCI6MTU3NTAwNjc2OCwiYWxnIjoiSFM1MTIifQ.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InlhbmdmYW4ifQ.yUb3PW53V89ZX4Ci2qeaBJIiizt0JUAN_W9BBzg8QkIR1-uO7NQl6jizSUReOFGanWzfG19t7XFHCWv1JGMIZw # =========解密token============ res = t.loads('eyJhbGciOiJIUzUxMiIsImV4cCI6MTU3NTAwNzM0MywiaWF0IjoxNTc1MDA2NzQzfQ.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InlhbmdmYW4ifQ.k-Q1VyN2TOlQ4flHHoiOYEMRaUEiN5Ms2JgeRdnCZWbQB-WwQ1FScoBWxFGkCYEPoWVpAjQxDBQeBesmulZupQ') # res = t.loads(token) print(res) # {'username': 'yangfan', 'user_id': 1} # 当超时或值有误则会报错
import itsdangerous salt='sdaf'#加盐 t=itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=600)#过期时间600秒 info = {'username':'yangfan','user_id':1} # =========加密token============ res=t.dumps(info) token=res.decode()#指定编码格式 print(token) # eyJhbGciOiJIUzUxMiIsImlhdCI6MTU0MTgxOTcyMCwiZXhwIjoxNTQxODIwMzIwfQ.eyJ1c2VybmFtZSI6InlhbmdmYW4iLCJ1c2VyX2lkIjoxfQ.VjCgry9Sr-4iRsK_MHYThcn_O7js9BERrXzocc7BI1aavC3N3s3e0wWMsvq2-Qp-ol_WNMD23wxiYRrA1kwCbg # =========解密token============ res = t.loads(token) print(res) # {'username': 'yangfan', 'user_id': 1} # 当超时或值有误则会报错