赞
踩
使用itsdangerous生成临时身份令牌
安装:pip install itsdangerous -i https://pypi.tuna.tsinghua.edu.cn/simple/
使用
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} # 若超时或值有误则会报错 try: res = t.loads(token) activate_user = User.objects.get(id=res['confirm']) activate_user.is_active = 1 activate_user.save() return HttpResponse('恭喜您,帐号已成功激活,快去登录挑选您喜爱的商品吧~(●ˇ∀ˇ●)') except SignatureExpired as e: return HttpResponse('链接已过期')
转载自:https://blog.csdn.net/weixin_43670190/article/details/122960967
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。