首页 > 解决方案 > 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"
    });

这是在应用程序前端编写的代码。

标签: expressauthenticationjwtauth0

解决方案


我们的一位高级工程师对此提出了很好的建议,我将在下面分享:

如果您还没有这样做,您应该执行以下步骤:

  1. 捕获您收到的令牌;例如,在开发中使用 console.log 语句。
  2. 查看捕获的令牌;它看起来像 JWT 吗?
  3. 如果它是 JWT 并且您可以在 jwt.io 解析它,那么在尝试验证之前更新 API 以记录接收到的令牌;收到的令牌是否与您期望的相符?

总之,尝试采取措施让您收集更多信息。


推荐阅读