简体   繁体   English

nodejs-JSONWebToken不会过期

[英]nodejs - JSONWebToken does not expire

I implented jsonwebtoken ( https://www.npmjs.com/package/jsonwebtoken ) and made two simple functions. 我对jsonwebtoken( https://www.npmjs.com/package/jsonwebtoken )提出了警告,并做了两个简单的功能。 One to create/sign a token with an expiration time and one to verify the token. 一种用于创建/签名具有到期时间的令牌,另一种用于验证令牌。

I use jwt.sign('userdata', 'abc', {expiresInSeconds: 1}); 我使用jwt.sign('userdata', 'abc', {expiresInSeconds: 1}); to sign the token. 签署令牌。 The token should expire after one second, but when I verify the token using jwt.verify(token, 'abc', function(err, decoded) { } the var err is empty and decoded returns userdata . 令牌应该在一秒钟后过期,但是当我使用jwt.verify(token, 'abc', function(err, decoded) { }验证jwt.verify(token, 'abc', function(err, decoded) { } var err为空,并且decoded返回userdata

The jsonwebtoken library only allows you to sign Objects , not Strings. jsonwebtoken库仅允许您签署对象 ,而不是字符串。 An issue suggests that the spec is a little unclear on this, but the library chose to only operate on Objects. 一个问题表明规范对此尚不明确,但该库选择仅对对象进行操作。

This code works as expected. 此代码按预期方式工作。 Note that I have replaced the string with an Object. 请注意,我已经用对象替换了字符串。

token = jwt.sign({ data: 'userdata' }, 'abc', {expiresInSeconds: 1});
setTimeout(function() {
  jwt.verify(token, 'abc', function(err, decoded) {
    console.log(err, decoded);
  })
}, 2000);

Output: 输出:

{ name: 'TokenExpiredError',
  message: 'jwt expired',
  expiredAt: Sun Apr 19 2015 11:08:36 GMT-0700 (Pacific Daylight Time) }

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

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