Python生成token

pip install PyJWT

encoded = jwt.encode({"account": "13146152021","overdue_time":"1655827200"}, "secret", algorithm="HS256")
print(encoded)

res=jwt.decode(encoded, "secret", algorithms=["HS256"])

print(res)
##打印结果为:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50IjoiMTMxNDYxNTIwMjEiLCJvdmVyZHVlX3RpbWUiOiIxNjU1ODI3MjAwIn0.VhPfiC0OC481eGmppxTa_OOLEyV-Itp6OGLeBe5ItXY
{'account': '13146152021', 'overdue_time': '1655827200'}

class Assist_Get_Data():
    '''生成、检验本地token、辅佐获取系统数据'''
    
    def judge_login(self,account,own_token):
        '''判断是否登录有效'''
        ###检验own_token是否有效
        judge_own_token=self.verify_token(own_token,account)
        
        ###如果无效返回False
        if not judge_own_token:
            return False

        ###如果有效则获取token
        correlation_account=models.User_Account.objects.filter(account=account).first()
        token=models.Login_Record.objects.filter(account=correlation_account).first().token
        return token
    
    def time_str_int(self,date_str):
        '''将时间字符串转为时间戳'''
        ##将时间字符串转为时间数组
        timeArray = time.strptime(date_str, "%Y-%m-%d")
        ##将时间数组转为时间戳
        timeStamp = int(time.mktime(timeArray))
        return timeStamp
    
    
    def create_token(self,account):
        '''生成本地token'''
        ##获取一天后的时间戳,token过期时间为一天后
        overdue_time=(datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        overdue_timeStamp=self.time_str_int(overdue_time)
        ###生成token
        own_token = jwt.encode({"account": account,"overdue_time":overdue_timeStamp}, SECRET_KEY, algorithm="HS256")
        return own_token
    
    def verify_token(self,own_token,account):
        '''检验token'''
        ##解密token
        decode_token=jwt.decode(own_token, SECRET_KEY, algorithms=["HS256"])
        ##获取token中的account
        token_account=decode_token['account']
        ##获取token中的过期时间
        token_overdue_time=decode_token['overdue_time']
        ##获取当前时间的时间
        now_time=datetime.datetime.now().strftime("%Y-%m-%d")
        ##转为时间戳
        now_timeStamp=self.time_str_int(now_time)
        ###进行判断token,如果账号对的上以及当前访问时间没有超过token过期时间则返回True
        if token_account==account and now_timeStamp<token_overdue_time:
            return True
        else:
            return False
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值