当前位置:   article > 正文

使用python itsdangerous生成临时身份令牌

itsdangerous

使用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('链接已过期')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

转载自:https://blog.csdn.net/weixin_43670190/article/details/122960967

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/505947
推荐阅读
相关标签
  

闽ICP备14008679号