{"record":{"id":"af036cd2f64a4d89","repo":"flipped-aurora/gin-vue-admin","slug":"token-af036c","errorCode":null,"errorMessage":"无法处理此token","messagePattern":"无法处理此token","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/jwt.go","lineNumber":23,"sourceCode":"\t\"errors\"\n\t\"time\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/global\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system/request\"\n\tjwt \"github.com/golang-jwt/jwt/v5\"\n)\n\ntype JWT struct {\n\tSigningKey []byte\n}\n\nvar (\n\tTokenValid            = errors.New(\"未知错误\")\n\tTokenExpired          = errors.New(\"token已过期\")\n\tTokenNotValidYet      = errors.New(\"token尚未激活\")\n\tTokenMalformed        = errors.New(\"这不是一个token\")\n\tTokenSignatureInvalid = errors.New(\"无效签名\")\n\tTokenInvalid          = errors.New(\"无法处理此token\")\n)\n\nfunc NewJWT() *JWT {\n\treturn &JWT{\n\t\t[]byte(global.GVA_CONFIG.JWT.SigningKey),\n\t}\n}\n\nfunc (j *JWT) CreateClaims(baseClaims request.BaseClaims) request.CustomClaims {\n\tbf, _ := ParseDuration(global.GVA_CONFIG.JWT.BufferTime)\n\tep, _ := ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)\n\tclaims := request.CustomClaims{\n\t\tBaseClaims: baseClaims,\n\t\tBufferTime: int64(bf / time.Second), // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失\n\t\tRegisteredClaims: jwt.RegisteredClaims{\n\t\t\tAudience:  jwt.ClaimStrings{\"GVA\"},                   // 受众\n\t\t\tNotBefore: jwt.NewNumericDate(time.Now().Add(-1000)), // 签名生效时间\n\t\t\tExpiresAt: jwt.NewNumericDate(time.Now().Add(ep)),    // 过期时间 7天  配置文件","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/jwt.go#L5-L41","documentation":"TokenInvalid is the generic 'cannot process this token' sentinel returned by ParseToken for jwt library errors that indicate the token is unusable but do not match the more specific sentinels (expired, not-valid-yet, malformed, signature). It is thejwt parse fallback for structurally rejected tokens.","triggerScenarios":"jwt.Parse returns an error outside the known switch in ParseToken — e.g. unsupported signing method (alg not HS256-style expected by this code), claim-type mismatches, or library-specific validation errors after token structure and signature checks.","commonSituations":"Tokens minted by another implementation using an unexpected alg; claims with unexpected types (e.g. exp as string not number); upgrading golang-jwt introduces new validation errors mapped to the default branch.","solutions":["Log the raw jwt error inside ParseToken to identify which validation failed and map it to a proper sentinel.","Re-issue the token with the expected signing method (HS256 with the configured key) and correctly typed claims (numeric exp/nbf).","Verify the token issuer uses the same golang-jwt major version and claim conventions.","If a library upgrade introduced it, check the jwt library migration notes for new default validations."],"exampleFix":"// before\ntoken := makeTokenWithAlg(\"none\") // unexpected signing method\nclaims, err := utils.ParseToken(token) // 无法处理此token\n// after\ntoken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) // expected alg\nsigned, _ := token.SignedString(key)\nclaims, err := utils.ParseToken(signed) // ok","handlingStrategy":"try-catch","validationCode":"header := parts[0]\nalg := base64DecodeJSON(header)[\"alg\"]\nif alg != \"HS256\" {\n    return errors.New(\"unsupported signing method\")\n}","typeGuard":null,"tryCatchPattern":"claims, err := utils.ParseToken(token)\nif err != nil {\n    switch {\n    case errors.Is(err, utils.TokenExpired):\n        handleExpired()\n    case errors.Is(err, utils.TokenInvalid):\n        // generic rejection; log raw jwt error to identify the unmapped cause\n        log.Warn(\"token invalid: %v\", rawErr)\n        reject401()\n    }\n}","preventionTips":["Issue tokens with the same jwt library version and HS256 signing method used for validation","Ensure claims use expected types (numeric exp/nbf/iat)","Keep ParseToken's error mapping in sync with the jwt library version in go.mod when upgrading"],"tags":["jwt","auth","go","token-validation"],"backgroundTag":"jwt-token-invalid","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}