跳到主要内容
ecdsa.com
golang-jwt/jwt (Go)Go

错误信息

token is unverifiable: error while executing keyfunc: kid "2026-10" not found in key set

也可能输出为

  • token is unverifiable: error while executing keyfunc
  • token is unverifiable: no keyfunc was provided

“error while executing keyfunc:”之后的文本就是你的 Keyfunc 返回的内容。用 nil Keyfunc 调用 `jwt.Parse`,会得到“token is unverifiable: no keyfunc was provided”。

golang-jwt/jwt (Go) — jwt.ErrTokenUnverifiable wrapping the error your Keyfunc returned

含义

golang-jwt 无法获取用于验证令牌的密钥:你的 `Keyfunc` 返回了错误,golang-jwt 把它包装成了 `ErrTokenUnverifiable`(“error while executing keyfunc: …”)。该前缀之后的内容都是你自己的 Keyfunc 给出的消息——在复现中,是按 `kid` 查找密钥却一无所获。令牌本身既没有通过验证,也没有因自身问题被拒绝。

原因

修复方法

  1. 1.

    使用能自动刷新 JWKS 的 Keyfunc

    基于 JWKS 的 Keyfunc 会在后台刷新密钥,并在出现未知 `kid` 时立即刷新,因此密钥轮换不会导致服务中断。

    go
    k, err := keyfunc.NewDefaultCtx(ctx, []string{"https://issuer.example.com/.well-known/jwks.json"})
    if err != nil {
    	log.Fatalf("load JWKS: %v", err)
    }
    // Refetches the JWKS in the background and on unknown kids (rate-limited).
    token, err := jwt.Parse(raw, k.Keyfunc, jwt.WithValidMethods([]string{"ES256"}))
  2. 2.

    记录被包装的原因

    `errors.Is(err, jwt.ErrTokenUnverifiable)` 用于识别错误类别;被包装的错误则告诉你是没查到密钥还是获取失败。

    go
    if errors.Is(err, jwt.ErrTokenUnverifiable) {
    	// The wrapped error is whatever your Keyfunc returned: log it, answer 401.
    	log.Printf("cannot verify token: %v", errors.Unwrap(err))
    }

相关错误

浏览完整的签名错误库