【问题标题】:NodeJS JWT Verify Fail even token if it's the same as the sign token如果它与签名令牌相同,NodeJS JWT 验证即使令牌也失败
【发布时间】:2017-05-14 22:55:46
【问题描述】:

尝试在 NodeJS 上开发我的 API,我得到了我的签名令牌并将其发送回安全的 api 路由,但 jwt 永远不会有效,即使它与我生成的令牌相同!我的代码有什么问题?

我就是这样签的

pbkdf2(queryPassword, salt, 10000, length, digest, (err: Error, hash: Buffer) => {
  if (hash.toString('hex') === userPassword) {
    sign({'user': username, permissions: []}, secret, {expiresIn: '7d'}, (err, token => {
      response.json({'token': token});
    }));
  } else {
    response.json({'error': 'User / Password Mismatch'});
  }
});

这里是验证:

verify(token, secret, function(tokenError, decoded) {
    if (tokenError) { // i'm always getting error...
        return response.status(403).json({
            message: 'Invalid token, please Log in first'
        });
    }

    next();
});

这是我的 Angular2 服务,它从我的 API 请求数据

let headers = new Headers({'Authorization': 'Bearer ' + this.token});
let options = new RequestOptions({headers: headers});
this.http.get(apiUrl, options);

符号生成的token:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoidHQiLCJwZXJtaXNzaW9ucyI6W10sImlhdCI6MTQ4MzExNTAzNCwiZXhwIjoxNDgzNzE5ODM0fQ.bJbH4619JAU8pf_6qPBRYeXb16pf_6qPBRYeXbeV10s

http 服务收到的令牌:

持票人exaiojkv1qilcjhbgcijiuzi1nij9.eyj1c2vyijoidhqilcjwzxjtaxnzaw9ucyi6w10simlhdci6mtq4mzexntazncwizxhwijoxndgznze5odm0fq.bjbh4619jau8pf_6qcyl0v1v5pxwspbryexbeb6vl_m

【问题讨论】:

    标签: node.js jwt express-jwt


    【解决方案1】:

    我终于找到了我的错误......

    在我的 angular2 Api 中,我使用了这一行

    let headers = new Headers({'Authorization': 'Bearer ' + this.token});
    

    我只需要发送

    let headers = new Headers({'Authorization': this.token});
    

    'Bearer' 导致我出错...

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 2017-06-22
      • 2018-09-30
      • 2015-10-16
      • 2019-11-01
      • 2016-01-17
      • 1970-01-01
      • 2018-05-29
      • 2021-09-10
      相关资源
      最近更新 更多