我正在使用node.js和express.js快递-jwt模块,并且我设置了一个简单的 HTTP 服务器来测试所有内容:
这是涉及的节点代码:
app.set('port', process.env.PORT || 3000);
app.use(express.methodOverride());
app.use(allow_cross_domain);
app.use('/api', expressJwt({secret: '09qrjjwef923jnrge$5ndjwk'}));
app.use(express.json());
app.use(express.urlencoded());
app.use('/', express.static(__dirname + '/'));
app.use(function(err, req, res, next){
if (err.constructor.name === 'UnauthorizedError') {
res.send(401, 'Unauthorized');
}
});
app.get('login',function(req,res){
//...
jwt.sign(results.username+results.email, secret, { expiresInMinutes: 9000000000*9393939393393939393939 });
});
app.post('api/profile',function(req,res){
console.log(req.user); // this return undefined in console
res.send(req.user); // response is pending and dunno why it returns error in browser console
});
所以一旦我打开/login
我登录并将会话令牌发送到的 URLapi/post
,它会在浏览器控制台中返回此响应错误:
{"error":{"message":"invalid signature","code":"invalid_token","status":401,"inner":{}}}
我不明白为什么会发生这种情况,因为前端存储的令牌和JWT中的令牌是相同的。导致此错误的原因可能是什么?
标头示例POST
编辑至api/post
URL: