Bob*_*ing 5 node.js oauth-2.0 express jwt
我需要一个令牌来向服务器进行身份验证,我正在使用jsonwebtoken,但只有在令牌标头为
{
"alg": "RS256"
}
Run Code Online (Sandbox Code Playgroud)
并不是
{
"alg": "RS256",
"typ": "JWT"
}
Run Code Online (Sandbox Code Playgroud)
默认jsonwebtoken标头是第二个,我尝试按照此处的说明使用选项设置标头https://github.com/auth0/node-jsonwebtoken,但没有示例,所以也许我做错了,但noTimestamp选项正在工作,所以......文档中有这一行“可以通过 options.header 对象自定义标头。” ,我认为我不明白这一点。
jwt.sign(payload,
pvtKey,
{ algorithm: 'RS256', noTimestamp : true, header: {"alg": "RS256"} }, function(err, token) {
if (err) {
return res.status(500).send("Error1: "+ err);
}
console.log("Created token: " + token);
});
Run Code Online (Sandbox Code Playgroud)
但它只是不会改变任何东西,所以如果有人知道如何设置标题?
提前致谢
该函数jwt.sign()创建一个默认标头,如下所示:
{
"alg": <algorithm>,
"typ": "JWT"
}
Run Code Online (Sandbox Code Playgroud)
如果header存在参数,则会根据该参数创建包含其他键/值对的标头。如果参数包含不同的typ或值alg,则将采用参数中的值。而且,正如 Atul 的回答中提到的,如果将值设置为undefined,您还可以摆脱标准标头。在以下示例中,我typ通过设置来删除该密钥undefined并添加一个额外的密钥x:
jwt.sign(payload, pvtKey,
{ algorithm: 'RS256', noTimestamp : true, header: {"typ": undefined, "x":"y"} })
Run Code Online (Sandbox Code Playgroud)
结果是这个标题:
{
"alg": "RS256",
"x": "y"
}
Run Code Online (Sandbox Code Playgroud)
结论:它有效,您可以自定义标头并删除标准值(正如 Atul 在其答案中指出的那样)
| 归档时间: |
|
| 查看次数: |
5586 次 |
| 最近记录: |