express - UnauthorizedError:身份验证时出现 jwt 格式错误的错误
问题描述
我使用 Auth0 对我的反应应用程序的用户进行身份验证。我一直在尝试访问服务器端的资源,但我不断收到 UnauthorizedError: jwt malformed as an Error。
我在社区论坛上关注了以下主题:https ://community.auth0.com/t/unauthorizederror-jwt-malformed-in-express-js/7352 许多用户建议该指南的受众价值是错误的。指南中给出的受众是 https:///userinfo 但它应该是 '<a href="https://.auth0.com/api/v2/" rel="nofollow noreferrer">https://.auth0 .com/api/v2/',我已经进行了更改,但错误仍然存在。
这对某些人有效,但对我无效。这是我一直遵循的指南:https ://auth0.com/blog/react-tutorial-building-and-securing-your-first-app/
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://<domain>/.well-known/jwks.json`
}),
// Validate the audience and the issuer.
audience: "https://<something>/api/v2/",
issuer: `https://<something>/`,
algorithms: ["RS256"]
});
这是我在快递方面写的代码。
this.auth0 = new auth0.WebAuth({
// the following three lines MUST be updated
domain: "<Domain>",
audience: "https://<Something>/api/v2/",
clientID: "clientID",
redirectUri: "http://localhost:3000/callback",
responseType: "token",
scope: "openid"
});
这是在应用程序前端编写的代码。
解决方案
我们的一位高级工程师对此提出了很好的建议,我将在下面分享:
如果您还没有这样做,您应该执行以下步骤:
- 捕获您收到的令牌;例如,在开发中使用 console.log 语句。
- 查看捕获的令牌;它看起来像 JWT 吗?
- 如果它是 JWT 并且您可以在 jwt.io 解析它,那么在尝试验证之前更新 API 以记录接收到的令牌;收到的令牌是否与您期望的相符?
总之,尝试采取措施让您收集更多信息。
推荐阅读
- google-sheets - 谷歌表格将包含多个数据的多列转换为行中的详细信息
- python - 是否有一个函数来规范化字符串并将它们转换为整数/浮点数?
- python-3.x - 迭代列表字典中的任意两个元素
- c# - MassTransit 中是否有对命名空间消费者的开箱即用支持?
- python - 如何重用 wtform 在数据库中更新和添加记录
- python - 在 for 循环中附加多维列表,其中索引随循环迭代而变化
- python - 如何使用 PySpark 读取目录下的 Parquet 文件?
- android - 如何从 RecyclerView 保存 CheckBox 状态?
- java - 未找到 JNativeHook 类
- mongodb - mongodB:MapReduce 内部错误 发出值的大小超出了 104857600 字节的设置大小限制