# 需要安装pip install itsdangerous
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
# 自己设置的秘钥
secret_key = 'ihdskjghksdsafasdgfdf4356fd'
expires_in = 3600*24*30*12 # 一年
class SecretOauth(object):
def __init__(self):
self.serializer = Serializer(secret_key=secret_key, expires_in=expires_in)
# 加密
def dumps(self, content_dict):
token = self.serializer.dumps(content_dict).decode()
return token
# 解密
def loads(self, token):
try:
content_dict = self.serializer.loads(token)
except Exception as e:
print(e)
return None
return content_dict
if __name__ == '__main__':
res = SecretOauth().dumps(content_dict={'session_key': 'kdshgjkhskjd8', 'openid': 'hdskghkj884j'})
print('加密后:', res)
res2 = SecretOauth().loads(res)
print('解密后:', res2)
if res2 != None:
print("解密成功")
print(res2.get('session_key'))
print(res2['openid'])
print(type(res2))
else:
print('token失败,请检查token')
django中itsdangerous加解密
最新推荐文章于 2023-06-10 09:18:58 发布