我正在尝试在request.POST的标头中将一个jwt从server1.js传输到server2.js;但是,我从server2得到以下错误:
JsonWebTokenError:必须提供jwt
我在Postman的身体和头部提供jwt
。
是什么导致了这个问题?我如何解决它?这是使用JSON标头传输的正确语法吗?
(server1 - console)
token: { actor: 'LOG_SERVER',
[stdout] description: 'admin',
[stdout] country: 'Canada',
[stdout] ids:
[stdout] [ { id: 1508959916,
[stdout] signature: '4d4a542f-d738-4736-8df5-84575b4d90' } ] }
[stdout] token[ids]: [ { id: 1508959916,
[stdout] signature: '4d4a542f-d738-4736-8df5-84575b4d90' } ]
[stdout] - before request.post
[stdout] after verifyDCS
[stdout] start respondWithJSON
[stdout] end log/asset/audit
[stdout] undefined
(server1.js)
logServer.post('/log/asset/audit/', function(req,res){
console.log('--- starting log/asset/audit');
let assetID = req.jwtPayload.assetID;
let dcList = req.jwtPayload.datacenters; //DC from proxy's jwtPayload
logDB.collection('logs').find( {"transaction.assetID" : assetID, "transaction.type": "Upload"} ).toArray( (err, result) => {
let token = JSON.parse(JSON.stringify(self_jwt_body));
result.forEach(function(element, index, array) {
console.log('- starting result.forEach function');
let dcFromLog = element.transaction.dataCenter; //DC from mongodb
console.log(`dcFromLog: ${dcFromLog}`);
//take destination dc from these types of transactions
console.log(`element.transaction.type: ${element.transaction.type}`);
token["ids"]=[{
id : element._id,
signature: element.transaction.signature
//transactionSignature: element.transactionSignature
}];
let JSON_token = JSON.stringify(token);
console.log('token:', token);
console.log('token[ids]:', token['ids']);
console.log('- before request.post');
request.post({ //**HERE**
url: dc_link, //includes dc_IP, dc_port and /audit
headers:
{
json : {"jwt": jwt.sign({JSON_token}, jwtSecret) } , //**HERE
}, function(error,response,body){
if(error || (response.statusCode != 200)){
console.log((error ? error : body));
console.log("response:", response);
console.log("body:", body);
console.log("error in request.post");
return;
}
console.log("url:", dc_link);
//console.log(jwt.sign(token, jwtSecret, {expiresIn: '1h'}));
if(body.result != 0){
console.log("Error: " + body.description);
}
}
});
//respond with array of logs.
respondWithJSON(200, req.body.result, req.body.description, res, result);
console.log('end log/asset/audit');
});
});
(server2.js)
function authentication(req,res,next){
console.log('start authentication');
jwt.verify(req.get('jwt'),jwtSecret, function(err, decoded){ //**HERE
if(err){
if(err.name == "TokenExpiredError"){
respondWithJSON(401, 401, "Authorization expired.", res);
}else{
respondWithJSON(400, 400, err.name+" : "+err.message, res);
}
console.log(err.name + " : " + err.message);
return;
}else{
req.jwtPayload = decoded;
next();
}
});
}