【问题标题】:how to generate token expire itself after 24 hours?如何在 24 小时后自行生成令牌?
【发布时间】:2020-01-17 18:06:28
【问题描述】:

我正在尝试创建一种更好的方法来生成 24 小时后自行过期的令牌,但我想用 1 分钟的时间对其进行测试,以证明它确实在完成这项工作。我不知道这是否是正确的方法,但如果不能帮助我

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
from django.utils.crypto import constant_time_compare
from django.utils.http import base36_to_int


class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            six.text_type(user.pk) + six.text_type(timestamp) +
            six.text_type(user.is_active)
        )

    def check_token(self, user, token):
        # This is a copy and clean of the super check_token code removing what is not required in this case
        if not (user and token):
            return False
        try:
            ts_b36, _ = token.split("-")
            ts = base36_to_int(ts_b36)
        except ValueError:
            return False
        if (self._num_days(self._today()) - ts) > 1: # 1 day = 24 hours 
            return False
        return True

account_activation_token = AccountActivationTokenGenerator()

【问题讨论】:

    标签: django token


    【解决方案1】:

    因此,如果您使用生成器创建标准 JWT 令牌或类似令牌,这些令牌将作为会话 cookie 存储在浏览器中。您可以设置这些 cookie 的到期时间。您将令牌存储在会话中

    request.session['token']=token
    

    然后设置过期时间

    request.session.set_expiry(300) #this means 300 seconds or 5 minutes
    

    【讨论】:

    • 你弄明白了吗? @Aron-Imperial
    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2021-04-06
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多