此问题通常是由于传递给Auth0的JWT字符串具有不正确的格式而引起的。通过检查JWT字符串的内容,可以发现它通常只包含三部分 - 标头、数据和签名 - 并以英文句点分隔为一个字符串。解决此问题的方法是确保JWT字符串的格式正确,并使用Auth0提供的验证API对JWT字符串进行验证。以下是使用Node.js的示例代码:
const jwt = require('jsonwebtoken');
const jwksClient = require('jwks-rsa');
const client = jwksClient({
jwksUri: 'https://yourdomain.auth0.com/.well-known/jwks.json'
});
function getKey(header, callback) {
client.getSigningKey(header.kid, function(err, key) {
var signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
}
const options = {
algorithms: ['RS256'],
audience: 'your-audience',
issuer: 'https://yourdomain.auth0.com/',
};
jwt.verify(token, getKey, options, function(err, decoded) {
if (err) {
console.log(err);
} else {
console.log(decoded);
}
});