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("链接已过期")