Skip to content

类型与常量

NumericDate

go
type NumericDate struct {
    time.Time
}

JWT 数字日期值(Unix 时间戳)。有效范围为 0 至 253402300799(9999-12-31 23:59:59 UTC)。

struct

方法

方法签名说明
MarshalJSONfunc (date *NumericDate) MarshalJSON() ([]byte, error)序列化为 Unix 时间戳 JSON 数字;零时间或超出有效范围时返回 null
UnmarshalJSONfunc (date *NumericDate) UnmarshalJSON(b []byte) error从 JSON 数字或字符串解析 Unix 时间戳;拒绝负值和超出有效范围的值

StringOrSlice

go
type StringOrSlice []string

持有 []string,可从 JSON 字符串或字符串数组反序列化;单元素切片序列化为 JSON 字符串,多元素序列化为数组,符合 RFC 7519 §4.1.3。

type

方法

方法签名说明
MarshalJSONfunc (s StringOrSlice) MarshalJSON() ([]byte, error)单元素切片序列化为 JSON 字符串,多元素序列化为数组(符合 RFC 7519 §4.1.3)
UnmarshalJSONfunc (s *StringOrSlice) UnmarshalJSON(b []byte) error从 JSON 字符串或数组解析

SigningMethod

go
type SigningMethod string

签名算法类型。

type

ValidationError

go
type ValidationError struct {
    Field   string
    Message string
    Err     error
}

字段级验证失败错误。

struct

方法

方法签名说明
Errorfunc (e *ValidationError) Error() string错误消息
Unwrapfunc (e *ValidationError) Unwrap() error解包内部错误

RateLimiter

go
type RateLimiter struct { ... }

令牌桶限流器,实现 RateLimitProvider 接口。

struct

方法

方法签名说明
Allowfunc (rl *RateLimiter) Allow(key string) bool检查单次请求
AllowNfunc (rl *RateLimiter) AllowN(key string, n int) bool检查 n 次请求
Resetfunc (rl *RateLimiter) Reset(key string)重置指定 key
Closefunc (rl *RateLimiter) Close()释放资源

SystemClock

go
type SystemClock struct{}

系统时钟,ClockProvider 的默认实现。

struct

方法

方法签名说明
Nowfunc (SystemClock) Now() time.Time返回当前系统时间

FixedClock

go
type FixedClock struct {
    T time.Time
}

固定时间时钟,用于测试。

struct

字段

字段类型说明
Ttime.Time固定时间值

方法

方法签名说明
Nowfunc (c FixedClock) Now() time.Time返回固定时间

签名算法常量

go
const (
    SigningMethodHS256 SigningMethod = "HS256"
    SigningMethodHS384 SigningMethod = "HS384"
    SigningMethodHS512 SigningMethod = "HS512"

    SigningMethodRS256 SigningMethod = "RS256"
    SigningMethodRS384 SigningMethod = "RS384"
    SigningMethodRS512 SigningMethod = "RS512"

    SigningMethodPS256 SigningMethod = "PS256"
    SigningMethodPS384 SigningMethod = "PS384"
    SigningMethodPS512 SigningMethod = "PS512"

    SigningMethodES256 SigningMethod = "ES256"
    SigningMethodES384 SigningMethod = "ES384"
    SigningMethodES512 SigningMethod = "ES512"
)
常量算法类型
SigningMethodHS256"HS256"HMAC-SHA256对称
SigningMethodHS384"HS384"HMAC-SHA384对称
SigningMethodHS512"HS512"HMAC-SHA512对称
SigningMethodRS256"RS256"RSA-SHA256非对称
SigningMethodRS384"RS384"RSA-SHA384非对称
SigningMethodRS512"RS512"RSA-SHA512非对称
SigningMethodPS256"PS256"RSA-PSS-SHA256非对称
SigningMethodPS384"PS384"RSA-PSS-SHA384非对称
SigningMethodPS512"PS512"RSA-PSS-SHA512非对称
SigningMethodES256"ES256"ECDSA-SHA256非对称
SigningMethodES384"ES384"ECDSA-SHA384非对称
SigningMethodES512"ES512"ECDSA-SHA512非对称

令牌类型常量

go
const (
    TokenTypeAccess  = "access"
    TokenTypeRefresh = "refresh"
)

写入 RegisteredClaims.TokenType 字段的令牌类型常量。

常量说明
TokenTypeAccess"access"访问令牌
TokenTypeRefresh"refresh"刷新令牌