跳到主要内容
ecdsa.com
jsonwebtoken (Node.js)JWT 库

错误信息

jwt issuer invalid. expected: https://login.example.com/

“expected:”后面是你配置的内容;如果配置的是颁发者数组,各个值会用逗号连接。

jsonwebtoken (Node.js) — JsonWebTokenError from jwt.verify()

含义

令牌的 `iss` 声明不等于你传给 `jwt.verify()` 的 `issuer` 选项(或者令牌根本没有 `iss`)。“expected:”后面的文本是你配置的值,而不是令牌中的值。jsonwebtoken 按字符串精确比较,所以签名虽然有效,令牌声称的来源却不是你的服务所信任的。

原因

修复方法

  1. 1.

    以 JSON 形式打印令牌的 iss

    `JSON.stringify` 能让看不见的差异显现出来——末尾斜杠、空白字符、不同的路径。然后从 `/.well-known/openid-configuration` 中把提供商的 `issuer` 复制到你的配置中。

    js
    const { iss } = jwt.decode(token) ?? {};
    console.log(JSON.stringify(iss)); // compare byte for byte with your issuer option
  2. 2.

    只在有意为之时才接受多个颁发者

    `issuer` 选项接受数组。把它用于在两种写法之间或在你掌控的颁发者之间进行有计划的迁移——并为每个颁发者配上各自的密钥。

    js
    jwt.verify(token, publicKey, {
      algorithms: ["ES256"],
      issuer: ["https://login.example.com/", "https://login.example.com"], // during a migration only
    });

相关错误

浏览完整的签名错误库