首页 > 解决方案 > UnauthorizedError:无效签名 - nodejs

问题描述

到目前为止,我已成功获取从前端传递的 jwt 令牌 - 角度到 nodejs 服务器。但是,我得到了错误 -

  UnauthorizedError: invalid signature 

你能指出我的语法/代码有什么问题吗?

app.component.ts

var token = localStorage.getItem('token')
    return new Promise((resolve, reject) => {
      var headers= new HttpHeaders({'Authorization': 'Bearer ' + token});

 this.http.get(this.nodejsUrl + "getMongoDData/getSystem" +emptyObj, { headers:headers })

服务器.js

var cors = require('cors');
var expressJwt = require('express-jwt');
var session = require('express-session');

app.use(cors({
    'Access-Control-Allow-Headers' : 'Content-Type, Authorization'
}));

app.use(expressJwt({
    secret: config.secret,
    credentialsRequired: false,
    getToken: function (req) {

        if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
            return req.headers.authorization.split(' ')[1];
        } else if (req.query && req.query.token) {
            return req.query.token;
        }
        return null;
    }
})
    .unless({
        path: ['/forgotP/forgotPassword', '/login/authenticate',
        ]
    })
);

标签: javascriptnode.jsauthorizationtokenexpress-jwt

解决方案


推荐阅读