django笔记-激活链接加密

pip install itsdangerous

from itsdangerous import TimedJSONWebSignatureSerializer as ts
from itsdangerous import SignatureExpired
from django.http import HttpResponse
from django.conf import settings #可以将settings里的SECRET_KEY拿过来当盐用

t = ts(settings.SECRET_KEY,60) #加盐。这里的60是加密的时效,超过这个秒数后将无法解密,会抛出超时异常。
strinfo = "some string"
res = t.dumps(strinfo) #res是加密后的串
try :
t1 = t.loads(res) # t1是解密后的串 就是前面的strinfo。
  #do something........
except SignatureExpired as se:
return HttpResponse("链接已过期")

 

posted @ 2020-07-24 23:58  大牛等等我  阅读(190)  评论(0编辑  收藏  举报