---安装---
pip install itsdangerous
---封装方法---
from itsdangerous.jws import TimedJSONWebSignatureSerializer
from django.conf import settings
# 加密json
def dumps(json, expires):
serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=expires)
json_str = serializer.dumps(json) # 调用jws
return json_str
# 解密json
def loads(json_str, expires):
serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=expires)
try:
json = serializer.loads(json_str) # 调用jws
except:
return None
else:
return json |
|