【问题标题】:add 8 hours to the token issuance date代币发行日期增加 8 小时
【发布时间】:2020-07-13 14:36:44
【问题描述】:

我有这个功能:

export const isTokenValid = () => {
  const isTokenExist = localStorage.getItem("TOKEN_AUTH");
  if (!isTokenExist) return false;
  const token = isTokenExist.split(" ")[1];
  const jwt = JSON.parse(atob(token.split(".")[1]));
  const iat = (jwt && jwt.iat * 1000) || null;
  console.log(iat);
  console.log(Date.now());
  const isExp = Date.now() > iat;
  if (isExp) {
    // localStorage.clear();
    return false;
  }
  return true;
};

在 console.log() 中:

1516239022000
1585764070793

我必须检查从 (iat)+8 小时发出到现在的令牌是否有效。如何将 iat 值增加 8 小时。

【问题讨论】:

    标签: jwt token access-token


    【解决方案1】:

    JWT 中的时间戳是从 01.01.1970 00:00 UTC 开始计算的 UNIX 时间戳:https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 解释说数字日期用于 exp 声明(也用于 nbf(不是之前)和 iat(发布时间)声明)

    https://www.rfc-editor.org/rfc/rfc7519#section-2 定义数字日期:

    一个 JSON 数值,表示从 1970-01-01T00:00:00Z UTC 直到指定的 UTC 日期/时间,忽略 闰秒。

    3600 秒是一小时,因此您将 8*3600 = 28.800 添加到令牌 (1516239022) 的原始 iat 值。 但是,如您的代码所示,没有必要乘以 1000。

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多