简体   繁体   English

当到期时间为24h时,jwt.verify()返回jwt到期

[英]jwt.verify() returns jwt expired when the expiration is 24h

I used jwt to create a token: 我用jwt创建一个令牌:

const jwt = require('jsonwebtoken');
const token = jwt.sign({
filePath: "path/to/file"
}, 'secretKey', {
expiresIn: "24h"
});
try {
  console.log(token)
  var decoded = jwt.verify(token, 'secretKey');
} catch(err) {
 console.log(err)
}

jwt.header: jwt.header:

{
  "alg": "HS256",
  "typ": "JWT"
}

payload: 有效载荷:

{
  "filePath": "path",
  "iat": 1557833831,
  "exp": 1557920231
}

When I test the snippet code mentioned above in my real app, I got an error message: 当我在真实应用中测试上述代码段时,收到错误消息:

jwt expired

Using the jwt debugger , the token is valid and should expire after 24h. 使用jwt调试器 ,令牌是有效的,应在24小时后过期。 The error returned by verify() which checks the expiration. verify()返回的错误,用于检查到期时间。 How jwt checks the expiration? jwt如何检查到期时间? or it does not check it? 还是不检查呢?

So since the question is, how does jwt check the expiration date, it depends on basically on some properties that may be implemented according to the JWT RFC 因此,既然问题是,jwt如何检查到期日期,它基本上取决于可以根据JWT RFC实现的某些属性

One would be exp . 一个就是exp In case a token expires before the current datetime, then the JWT cannot be processed 如果令牌在当前日期时间之前过期,则无法处理JWT

The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. “ exp”(到期时间)声明标识了不得接受JWT进行处理的到期时间。 The processing of the "exp" claim requires that the current date/time MUST be before the expiration date/time listed in the "exp" claim. “ exp”声明的处理要求当前日期/时间必须早于“ exp”声明中列出的到期日期/时间。

Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. 实施者可以留出一些余地,通常不超过几分钟,以解决时钟偏差。 Its value MUST be a number containing a NumericDate value. 它的值必须是包含NumericDate值的数字。 Use of this claim is OPTIONAL. 此声明的使用是可选的。

Another one to note would be the iat , which stands for issued at 另一个要注意的是iat ,它表示

The "iat" (issued at) claim identifies the time at which the JWT was issued. “ iat”(发布于)声明标识了JWT的发布时间。 This claim can be used to determine the age of the JWT. 此声明可用于确定JWT的年龄。 Its value MUST be a number containing a NumericDate value. 它的值必须是包含NumericDate值的数字。 Use of this claim is OPTIONAL. 此声明的使用是可选的。

A final one that could be used for time verfication, as far as I am aware of would be, nbf , standing for not before 据我所知,可以用于时间验证的最后一个是nbf ,表示不早于

The "nbf" (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. “ nbf”(不早于)声明标识了不得接受JWT进行处理的时间。 The processing of the "nbf" claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the "nbf" claim. 处理“ nbf”声明时,要求当前日期/时间必须晚于或等于“ nbf”声明中列出的不早日期/时间。 Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. 实施者可以留出一些余地,通常不超过几分钟,以解决时钟偏差。 Its value MUST be a number containing a NumericDate value. 它的值必须是包含NumericDate值的数字。 Use of this claim is OPTIONAL. 此声明的使用是可选的。

Now, for the code at hand, I don't see anything which is of, having following setup, this works perfectly fine for me 现在,对于手头的代码,我看不到任何东西,进行以下设置后,对我来说一切正常

const jwt = require('jsonwebtoken');

const token = jwt.sign( {
  hello: 'world'   
}, 'myverysecretkey', {
    expiresIn: '24h'
});

try {
    const verify = jwt.verify( token, 'myverysecretkey' );
    console.log( verify );
} catch (err) {
    console.error( err );
}

which would output 将输出

Object {hello: "world", iat: 1557840459, exp: 1557926859}

This can be validated on the codesandbox link 可以在codeandbox链接上对此进行验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM