config

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 默认 Token 名称沿用 Java 版 "sa-token",便于跨端配置一致性
	DefaultTokenName     = "sa-token"
	DefaultTimeout       = 2592000 // 30 days in seconds | 30天(秒)
	DefaultMaxLoginCount = 12      // Maximum concurrent logins | 最大并发登录数
	DefaultCookiePath    = "/"
	NoLimit              = -1 // No limit flag | 不限制标志
)

Default configuration constants | 默认配置常量

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// TokenName Token name (also used as Cookie name) | Token名称(同时也是Cookie名称)
	TokenName string

	// Timeout Token expiration time in seconds, -1 for never expire | Token超时时间(单位:秒,-1代表永不过期)
	Timeout int64

	// ActiveTimeout Token minimum activity frequency in seconds. If Token is not accessed for this time, it will be frozen. -1 means no limit | Token最低活跃频率(单位:秒),如果Token超过此时间没有访问,则会被冻结。-1代表不限制,永不冻结
	ActiveTimeout int64

	// IsConcurrent Allow concurrent login for the same account (true=allow concurrent login, false=new login kicks out old login) | 是否允许同一账号并发登录(为true时允许一起登录,为false时新登录挤掉旧登录)
	IsConcurrent bool

	// IsShare Share the same Token for concurrent logins (true=share one Token, false=create new Token for each login) | 在多人登录同一账号时,是否共用一个Token(为true时所有登录共用一个Token,为false时每次登录新建一个Token)
	IsShare bool

	// MaxLoginCount Maximum number of concurrent logins for the same account, -1 means no limit (only effective when IsConcurrent=true and IsShare=false) | 同一账号最大登录数量,-1代表不限(只有在IsConcurrent=true,IsShare=false时此配置才有效)
	MaxLoginCount int

	// IsReadBody Try to read Token from request body (default: false) | 是否尝试从请求体里读取Token(默认:false)
	IsReadBody bool

	// IsReadHeader Try to read Token from HTTP Header (default: true, recommended) | 是否尝试从Header里读取Token(默认:true,推荐)
	IsReadHeader bool

	// IsReadCookie Try to read Token from Cookie (default: false) | 是否尝试从Cookie里读取Token(默认:false)
	IsReadCookie bool

	// TokenStyle Token generation style | Token风格
	TokenStyle TokenStyle

	// DataRefreshPeriod Auto-refresh period in seconds, -1 means no auto-refresh | 自动续签(单位:秒),-1代表不自动续签
	DataRefreshPeriod int64

	// TokenSessionCheckLogin Check if Token-Session is kicked out when logging in (true=check on login, false=skip check) | Token-Session在登录时是否检查(true=登录时验证是否被踢下线,false=不作此检查)
	TokenSessionCheckLogin bool

	// AutoRenew Auto-renew Token expiration time on each validation | 是否自动续期(每次验证Token时,都会延长Token的有效期)
	AutoRenew bool

	// JwtSecretKey JWT secret key (only effective when TokenStyle=JWT) | JWT密钥(只有TokenStyle=JWT时,此配置才生效)
	JwtSecretKey string

	// JwtAlgorithm JWT 签名算法,可选 HS256/HS384/HS512/RS256/RS384/RS512,默认 HS256
	JwtAlgorithm string

	// JwtPrivateKey RSA 私钥(PEM),当算法为 RS* 时必填
	JwtPrivateKey string

	// JwtPublicKey RSA 公钥(PEM),当算法为 RS* 时验签使用,可选;为空则使用私钥推导
	JwtPublicKey string

	// JwtKeyID 写入 header.kid,便于密钥识别,可选
	JwtKeyID string

	// JwtCustomClaims 追加的自定义声明 | 自定义声明
	JwtCustomClaims map[string]string

	// IsLog Enable operation logging | 是否输出操作日志
	IsLog bool

	// IsPrintBanner Print startup banner (default: true) | 是否打印启动 Banner(默认:true)
	IsPrintBanner bool

	// KeyPrefix Storage key prefix for Redis isolation (default: "satoken:") | 存储键前缀,用于Redis隔离(默认:"satoken:")
	// Set to empty "" to be compatible with Java sa-token default behavior | 设置为空""以兼容Java sa-token默认行为
	KeyPrefix string

	// CookieConfig Cookie configuration | Cookie配置
	CookieConfig *CookieConfig

	// RenewPoolConfig Configuration for renewal pool manager | 续期池配置
	RenewPoolConfig *pool.RenewPoolConfig
}

Config Sa-Token configuration | Sa-Token配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig Returns default configuration | 返回默认配置

func (*Config) Clone

func (c *Config) Clone() *Config

Clone Clone configuration | 克隆配置

func (*Config) SetActiveTimeout

func (c *Config) SetActiveTimeout(timeout int64) *Config

SetActiveTimeout Set active timeout duration | 设置活跃超时时间

func (*Config) SetAutoRenew

func (c *Config) SetAutoRenew(autoRenew bool) *Config

SetAutoRenew Set whether to auto-renew Token | 设置是否自动续期

func (*Config) SetCookieConfig

func (c *Config) SetCookieConfig(cookieConfig *CookieConfig) *Config

SetCookieConfig Set cookie configuration | 设置Cookie配置

func (*Config) SetDataRefreshPeriod

func (c *Config) SetDataRefreshPeriod(period int64) *Config

SetDataRefreshPeriod Set data refresh period | 设置数据刷新周期

func (*Config) SetIsConcurrent

func (c *Config) SetIsConcurrent(isConcurrent bool) *Config

SetIsConcurrent Set whether to allow concurrent login | 设置是否允许并发登录

func (*Config) SetIsLog

func (c *Config) SetIsLog(isLog bool) *Config

SetIsLog Set whether to enable logging | 设置是否输出日志

func (*Config) SetIsPrintBanner

func (c *Config) SetIsPrintBanner(isPrint bool) *Config

SetIsPrintBanner Set whether to print banner | 设置是否打印Banner

func (*Config) SetIsReadBody

func (c *Config) SetIsReadBody(isReadBody bool) *Config

SetIsReadBody Set whether to read Token from body | 设置是否从请求体读取Token

func (*Config) SetIsReadCookie

func (c *Config) SetIsReadCookie(isReadCookie bool) *Config

SetIsReadCookie Set whether to read Token from cookie | 设置是否从Cookie读取Token

func (*Config) SetIsReadHeader

func (c *Config) SetIsReadHeader(isReadHeader bool) *Config

SetIsReadHeader Set whether to read Token from header | 设置是否从Header读取Token

func (*Config) SetIsShare

func (c *Config) SetIsShare(isShare bool) *Config

SetIsShare Set whether to share Token | 设置是否共享Token

func (*Config) SetJwtSecretKey

func (c *Config) SetJwtSecretKey(key string) *Config

SetJwtSecretKey Set JWT secret key | 设置JWT密钥

func (*Config) SetKeyPrefix

func (c *Config) SetKeyPrefix(prefix string) *Config

SetKeyPrefix Set storage key prefix | 设置存储键前缀

func (*Config) SetMaxLoginCount

func (c *Config) SetMaxLoginCount(count int) *Config

SetMaxLoginCount Set maximum login count | 设置最大登录数量

func (*Config) SetRenewPoolConfig

func (c *Config) SetRenewPoolConfig(renewPoolConfig *pool.RenewPoolConfig) *Config

SetRenewPoolConfig Set renewal pool configuration | 设置续期池配置

func (*Config) SetTimeout

func (c *Config) SetTimeout(timeout int64) *Config

SetTimeout Set timeout duration | 设置超时时间

func (*Config) SetTokenName

func (c *Config) SetTokenName(name string) *Config

SetTokenName Set Token name | 设置Token名称

func (*Config) SetTokenSessionCheckLogin

func (c *Config) SetTokenSessionCheckLogin(check bool) *Config

SetTokenSessionCheckLogin Set whether to check token session on login | 设置登录时是否检查token会话

func (*Config) SetTokenStyle

func (c *Config) SetTokenStyle(style TokenStyle) *Config

SetTokenStyle Set Token generation style | 设置Token风格

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration | 验证配置是否合理

type CookieConfig

type CookieConfig struct {
	// Domain Cookie domain | 作用域
	Domain string

	// Path Cookie path | 路径
	Path string

	// Secure Only effective under HTTPS | 是否只在HTTPS下生效
	Secure bool

	// HttpOnly Prevent JavaScript access to Cookie | 是否禁止JS操作Cookie
	HttpOnly bool

	// SameSite SameSite attribute (Strict, Lax, None) | SameSite属性(Strict、Lax、None)
	SameSite SameSiteMode

	// MaxAge Cookie expiration time in seconds | 过期时间(单位:秒)
	MaxAge int
}

CookieConfig Cookie configuration | Cookie配置

type SameSiteMode

type SameSiteMode string

SameSiteMode Cookie SameSite attribute values | Cookie的SameSite属性值

const (
	// SameSiteStrict Strict mode | 严格模式
	SameSiteStrict SameSiteMode = "Strict"
	// SameSiteLax Lax mode | 宽松模式
	SameSiteLax SameSiteMode = "Lax"
	// SameSiteNone None mode | 无限制模式
	SameSiteNone SameSiteMode = "None"
)

type TokenStyle

type TokenStyle string

TokenStyle Token generation style | Token生成风格

const (
	// TokenStyleUUID UUID style | UUID风格
	TokenStyleUUID TokenStyle = "uuid"
	// TokenStyleSimple Simple random string | 简单随机字符串
	TokenStyleSimple TokenStyle = "simple"
	// TokenStyleRandom32 32-bit random string | 32位随机字符串
	TokenStyleRandom32 TokenStyle = "random32"
	// TokenStyleRandom64 64-bit random string | 64位随机字符串
	TokenStyleRandom64 TokenStyle = "random64"
	// TokenStyleRandom128 128-bit random string | 128位随机字符串
	TokenStyleRandom128 TokenStyle = "random128"
	// TokenStyleJWT JWT style | JWT风格
	TokenStyleJWT TokenStyle = "jwt"
	// TokenStyleHash SHA256 hash-based style | SHA256哈希风格
	TokenStyleHash TokenStyle = "hash"
	// TokenStyleTimestamp Timestamp-based style | 时间戳风格
	TokenStyleTimestamp TokenStyle = "timestamp"
	// TokenStyleTik Short ID style (like TikTok) | Tik风格短ID(类似抖音)
	TokenStyleTik TokenStyle = "tik"
)

func (TokenStyle) IsValid

func (ts TokenStyle) IsValid() bool

IsValid checks if the TokenStyle is valid | 检查TokenStyle是否有效

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL