sa

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: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TagSaCheckLogin      = "sa_check_login"
	TagSaCheckRole       = "sa_check_role"
	TagSaCheckPermission = "sa_check_permission"
	TagSaCheckDisable    = "sa_check_disable"
	TagSaIgnore          = "sa_ignore"
)

Annotation constants | 注解常量

View Source
const (
	TokenStyleUUID      = core.TokenStyleUUID
	TokenStyleSimple    = core.TokenStyleSimple
	TokenStyleRandom32  = core.TokenStyleRandom32
	TokenStyleRandom64  = core.TokenStyleRandom64
	TokenStyleRandom128 = core.TokenStyleRandom128
	TokenStyleJWT       = core.TokenStyleJWT
	TokenStyleHash      = core.TokenStyleHash
	TokenStyleTimestamp = core.TokenStyleTimestamp
	TokenStyleTik       = core.TokenStyleTik
)

Token style constants | Token风格常量

View Source
const (
	EventLogin           = core.EventLogin
	EventLogout          = core.EventLogout
	EventKickout         = core.EventKickout
	EventDisable         = core.EventDisable
	EventUntie           = core.EventUntie
	EventRenew           = core.EventRenew
	EventCreateSession   = core.EventCreateSession
	EventDestroySession  = core.EventDestroySession
	EventPermissionCheck = core.EventPermissionCheck
	EventRoleCheck       = core.EventRoleCheck
	EventAll             = core.EventAll
)

Event constants | 事件常量

View Source
const (
	GrantTypeAuthorizationCode = core.GrantTypeAuthorizationCode
	GrantTypeRefreshToken      = core.GrantTypeRefreshToken
	GrantTypeClientCredentials = core.GrantTypeClientCredentials
	GrantTypePassword          = core.GrantTypePassword
)

OAuth2 grant type constants | OAuth2授权类型常量

View Source
const Version = core.Version

Version Sa-Token-Go version | Sa-Token-Go版本

Variables

View Source
var (
	RandomString   = core.RandomString
	IsEmpty        = core.IsEmpty
	IsNotEmpty     = core.IsNotEmpty
	DefaultString  = core.DefaultString
	ContainsString = core.ContainsString
	RemoveString   = core.RemoveString
	UniqueStrings  = core.UniqueStrings
	MergeStrings   = core.MergeStrings
	MatchPattern   = core.MatchPattern
)

Utility functions | 工具函数

Functions

func CheckDisable

func CheckDisable() gin.HandlerFunc

CheckDisable decorator for checking if account is disabled | 检查是否被封禁装饰器

func CheckDisableByToken

func CheckDisableByToken(tokenValue string) error

CheckDisableByToken checks if account is disabled (throws error if disabled) | 检查Token对应账号是否被封禁(被封禁则抛出错误)

func CheckLogin

func CheckLogin() gin.HandlerFunc

CheckLogin decorator for login checking | 检查登录装饰器

func CheckLoginByToken

func CheckLoginByToken(tokenValue string) error

CheckLoginByToken checks login status (throws error if not logged in) | 检查登录状态(未登录抛出错误)

func CheckPermission

func CheckPermission(perms ...string) gin.HandlerFunc

CheckPermission decorator for permission checking | 检查权限装饰器

func CheckPermissionAndByToken

func CheckPermissionAndByToken(tokenValue string, permissions []string) error

CheckPermissionAndByToken checks if the token has all specified permissions (AND logic) | 检查Token是否拥有所有指定权限(AND逻辑)

func CheckPermissionByToken

func CheckPermissionByToken(tokenValue string, permission string) error

CheckPermissionByToken checks if the token has specified permission | 检查Token是否拥有指定权限

func CheckPermissionOrByToken

func CheckPermissionOrByToken(tokenValue string, permissions []string) error

CheckPermissionOrByToken checks if the token has any of the specified permissions (OR logic) | 检查Token是否拥有指定权限中的任意一个(OR逻辑)

func CheckRole

func CheckRole(roles ...string) gin.HandlerFunc

CheckRole decorator for role checking | 检查角色装饰器

func CheckRoleAndByToken

func CheckRoleAndByToken(tokenValue string, roles []string) error

CheckRoleAndByToken checks if the token has all specified roles (AND logic) | 检查Token是否拥有所有指定角色(AND逻辑)

func CheckRoleByToken

func CheckRoleByToken(tokenValue string, role string) error

CheckRoleByToken checks if the token has specified role | 检查Token是否拥有指定角色

func CheckRoleOrByToken

func CheckRoleOrByToken(tokenValue string, roles []string) error

CheckRoleOrByToken checks if the token has any of the specified roles (OR logic) | 检查Token是否拥有指定角色中的任意一个(OR逻辑)

func Disable

func Disable(loginID interface{}, duration time.Duration) error

Disable disables an account for specified duration | 封禁账号(指定时长)

func GenerateNonce

func GenerateNonce() (string, error)

GenerateNonce generates a new nonce token | 生成新的Nonce令牌

func GetDisableTime

func GetDisableTime(loginID interface{}) (int64, error)

GetDisableTime gets remaining disabled time | 获取账号剩余封禁时间

func GetHandler

func GetHandler(handler interface{}, annotations ...*Annotation) gin.HandlerFunc

GetHandler gets handler with annotations | 获取带注解的处理器

func GetLoginID

func GetLoginID(tokenValue string) (string, error)

GetLoginID gets the login ID from token | 从Token获取登录ID

func GetLoginIDNotCheck

func GetLoginIDNotCheck(tokenValue string) (string, error)

GetLoginIDNotCheck gets login ID without checking | 获取登录ID(不检查)

func GetPermissionListByToken

func GetPermissionListByToken(tokenValue string) ([]string, error)

GetPermissionListByToken gets the permission list for a token | 获取Token的权限列表

func GetRoleListByToken

func GetRoleListByToken(tokenValue string) ([]string, error)

GetRoleListByToken gets the role list for a token | 获取Token的角色列表

func GetSaToken

func GetSaToken(c *gin.Context) (*core.SaTokenContext, bool)

GetSaToken gets Sa-Token context from Gin context | 从Gin上下文获取Sa-Token上下文

func GetTokenValue

func GetTokenValue(loginID interface{}, device ...string) (string, error)

GetTokenValue gets the token value for a login ID | 获取登录ID对应的Token值

func HasPermission

func HasPermission(loginID interface{}, permission string) bool

HasPermission checks if the account has specified permission (returns bool) | 检查账号是否拥有指定权限(返回布尔值)

func HasRole

func HasRole(loginID interface{}, role string) bool

HasRole checks if the account has specified role (returns bool) | 检查账号是否拥有指定角色(返回布尔值)

func Ignore

func Ignore() gin.HandlerFunc

Ignore decorator to ignore authentication | 忽略认证装饰器

func IsDisable

func IsDisable(loginID interface{}) bool

IsDisable checks if an account is disabled | 检查账号是否被封禁

func IsLogin

func IsLogin(tokenValue string) bool

IsLogin checks if the user is logged in | 检查用户是否已登录

func Kickout

func Kickout(loginID interface{}, device ...string) error

Kickout kicks out a user session | 踢人下线

func Login

func Login(loginID interface{}, device ...string) (string, error)

Login performs user login | 用户登录

func LoginByToken

func LoginByToken(loginID interface{}, tokenValue string, device ...string) error

LoginByToken performs login with specified token | 使用指定Token登录

func Logout

func Logout(loginID interface{}, device ...string) error

Logout performs user logout | 用户登出

func LogoutByToken

func LogoutByToken(tokenValue string) error

LogoutByToken performs logout by token | 根据Token登出

func Middleware

func Middleware(annotations ...*Annotation) gin.HandlerFunc

Middleware 创建中间件版本

func NewGinContext

func NewGinContext(c *gin.Context) adapter.RequestContext

NewGinContext creates a Gin context adapter | 创建Gin上下文适配器

func ProcessStructAnnotations

func ProcessStructAnnotations(handler interface{}) gin.HandlerFunc

ProcessStructAnnotations processes annotations on struct tags | 处理结构体上的注解标签

func RevokeRefreshToken

func RevokeRefreshToken(refreshToken string) error

RevokeRefreshToken revokes a refresh token | 撤销刷新令牌

func SetManager

func SetManager(mgr *Manager)

SetManager sets the global Manager (must be called first) | 设置全局Manager(必须先调用此方法)

func Untie

func Untie(loginID interface{}) error

Untie unties/unlocks an account | 解除账号封禁

func UseStorage

func UseStorage(storage Storage, cfg *Config)

UseStorage 允许直接传入自定义存储实例

func UseStorageProvider

func UseStorageProvider(provider StorageProvider, cfg *Config)

UseStorageProvider 允许用户在运行时切换默认存储实现

func VerifyNonce

func VerifyNonce(nonce string) bool

VerifyNonce verifies a nonce token | 验证Nonce令牌

func WithAnnotation

func WithAnnotation(ann *Annotation) gin.HandlerFunc

WithAnnotation decorator with custom annotation | 使用自定义注解装饰器

Types

type Annotation

type Annotation struct {
	CheckLogin      bool     `json:"checkLogin"`
	CheckRole       []string `json:"checkRole"`
	CheckPermission []string `json:"checkPermission"`
	CheckDisable    bool     `json:"checkDisable"`
	Ignore          bool     `json:"ignore"`
}

Annotation annotation structure | 注解结构体

func ParseTag

func ParseTag(tag string) *Annotation

ParseTag parses struct tags | 解析结构体标签

func (*Annotation) Validate

func (a *Annotation) Validate() bool

Validate validates if annotation is valid | 验证注解是否有效

type Builder

type Builder = core.Builder

Core types | 核心类型

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new builder for fluent configuration | 创建新的Builder构建器(用于流式配置)

type Config

type Config = core.Config

Configuration related types | 配置相关类型

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default configuration | 返回默认配置

type CookieConfig

type CookieConfig = core.CookieConfig

Configuration related types | 配置相关类型

type Event

type Event = core.Event

Event related types | 事件相关类型

type EventData

type EventData = core.EventData

Event related types | 事件相关类型

type EventListener

type EventListener = core.EventListener

Event related types | 事件相关类型

type EventManager

type EventManager = core.EventManager

Event related types | 事件相关类型

func NewEventManager

func NewEventManager() *EventManager

NewEventManager creates a new event manager | 创建新的事件管理器

type GinContext

type GinContext struct {
	// contains filtered or unexported fields
}

GinContext Gin request context adapter | Gin请求上下文适配器

func (*GinContext) Abort

func (g *GinContext) Abort()

Abort implements adapter.RequestContext.

func (*GinContext) Get

func (g *GinContext) Get(key string) (interface{}, bool)

Get gets context value | 获取上下文值

func (*GinContext) GetBody

func (g *GinContext) GetBody() ([]byte, error)

GetBody implements adapter.RequestContext.

func (*GinContext) GetClientIP

func (g *GinContext) GetClientIP() string

GetClientIP gets client IP address | 获取客户端IP地址

func (*GinContext) GetCookie

func (g *GinContext) GetCookie(key string) string

GetCookie gets cookie | 获取Cookie

func (*GinContext) GetHeader

func (g *GinContext) GetHeader(key string) string

GetHeader gets request header | 获取请求头

func (*GinContext) GetHeaders

func (g *GinContext) GetHeaders() map[string][]string

GetHeaders implements adapter.RequestContext.

func (*GinContext) GetMethod

func (g *GinContext) GetMethod() string

GetMethod gets request method | 获取请求方法

func (*GinContext) GetPath

func (g *GinContext) GetPath() string

GetPath gets request path | 获取请求路径

func (*GinContext) GetPostForm

func (g *GinContext) GetPostForm(key string) string

GetPostForm implements adapter.RequestContext.

func (*GinContext) GetQuery

func (g *GinContext) GetQuery(key string) string

GetQuery gets query parameter | 获取查询参数

func (*GinContext) GetQueryAll

func (g *GinContext) GetQueryAll() map[string][]string

GetQueryAll implements adapter.RequestContext.

func (*GinContext) GetString

func (g *GinContext) GetString(key string) string

GetString implements adapter.RequestContext.

func (*GinContext) GetURL

func (g *GinContext) GetURL() string

GetURL implements adapter.RequestContext.

func (*GinContext) GetUserAgent

func (g *GinContext) GetUserAgent() string

GetUserAgent implements adapter.RequestContext.

func (*GinContext) IsAborted

func (g *GinContext) IsAborted() bool

IsAborted implements adapter.RequestContext.

func (*GinContext) MustGet

func (g *GinContext) MustGet(key string) any

MustGet implements adapter.RequestContext.

func (*GinContext) Set

func (g *GinContext) Set(key string, value interface{})

Set sets context value | 设置上下文值

func (*GinContext) SetCookie

func (g *GinContext) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

SetCookie sets cookie | 设置Cookie

func (*GinContext) SetCookieWithOptions

func (g *GinContext) SetCookieWithOptions(options *adapter.CookieOptions)

SetCookieWithOptions implements adapter.RequestContext.

func (*GinContext) SetHeader

func (g *GinContext) SetHeader(key, value string)

SetHeader sets response header | 设置响应头

type HandlerWithAnnotations

type HandlerWithAnnotations struct {
	Handler     interface{}
	Annotations []*Annotation
}

HandlerWithAnnotations 带注解的处理器包装器

func NewHandlerWithAnnotations

func NewHandlerWithAnnotations(handler interface{}, annotations ...*Annotation) *HandlerWithAnnotations

NewHandlerWithAnnotations 创建带注解的处理器

func (*HandlerWithAnnotations) ToGinHandler

func (h *HandlerWithAnnotations) ToGinHandler() gin.HandlerFunc

ToGinHandler 转换为Gin处理器

type ListenerConfig

type ListenerConfig = core.ListenerConfig

Event related types | 事件相关类型

type ListenerFunc

type ListenerFunc = core.ListenerFunc

Event related types | 事件相关类型

type Manager

type Manager = core.Manager

Core types | 核心类型

func GetManager

func GetManager() *Manager

GetManager gets the global Manager | 获取全局Manager

func NewManager

func NewManager(storage Storage, cfg *Config) *Manager

NewManager creates a new authentication manager | 创建新的认证管理器

type NonceManager

type NonceManager = core.NonceManager

Core types | 核心类型

func NewNonceManager

func NewNonceManager(storage Storage, prefix string, ttl ...int64) *NonceManager

NewNonceManager creates a new nonce manager | 创建新的Nonce管理器

type OAuth2AccessToken

type OAuth2AccessToken = core.OAuth2AccessToken

Core types | 核心类型

type OAuth2Client

type OAuth2Client = core.OAuth2Client

Core types | 核心类型

type OAuth2GrantType

type OAuth2GrantType = core.OAuth2GrantType

Core types | 核心类型

type OAuth2Server

type OAuth2Server = core.OAuth2Server

Core types | 核心类型

func GetOAuth2Server

func GetOAuth2Server() *OAuth2Server

GetOAuth2Server gets the OAuth2 server instance | 获取OAuth2服务器实例

func NewOAuth2Server

func NewOAuth2Server(storage Storage, prefix string) *OAuth2Server

NewOAuth2Server creates a new OAuth2 server | 创建新的OAuth2服务器

type Plugin

type Plugin struct {
	ValidateCredentials func(username, password string) error
	// contains filtered or unexported fields
}

Plugin Gin plugin for Sa-Token | Gin插件

func NewPlugin

func NewPlugin(manager *core.Manager) *Plugin

NewPlugin creates a Gin plugin | 创建Gin插件

func (*Plugin) AuthMiddleware

func (p *Plugin) AuthMiddleware() gin.HandlerFunc

AuthMiddleware authentication middleware | 认证中间件

func (*Plugin) LoginHandler

func (p *Plugin) LoginHandler(c *gin.Context)

LoginHandler login handler example | 登录处理器示例

func (*Plugin) LogoutHandler

func (p *Plugin) LogoutHandler(c *gin.Context)

LogoutHandler logout handler | 登出处理器

func (*Plugin) PermissionRequired

func (p *Plugin) PermissionRequired(permission string) gin.HandlerFunc

PermissionRequired permission validation middleware | 权限验证中间件

func (*Plugin) RoleRequired

func (p *Plugin) RoleRequired(role string) gin.HandlerFunc

RoleRequired role validation middleware | 角色验证中间件

func (*Plugin) UserInfoHandler

func (p *Plugin) UserInfoHandler(c *gin.Context)

UserInfoHandler user info handler example | 获取用户信息处理器示例

type RefreshTokenInfo

type RefreshTokenInfo = core.RefreshTokenInfo

Core types | 核心类型

func LoginWithRefreshToken

func LoginWithRefreshToken(loginID interface{}, device ...string) (*RefreshTokenInfo, error)

LoginWithRefreshToken performs login and returns refresh token info | 登录并返回刷新令牌信息

func RefreshAccessToken

func RefreshAccessToken(refreshToken string) (*RefreshTokenInfo, error)

RefreshAccessToken refreshes the access token using a refresh token | 使用刷新令牌刷新访问令牌

type RefreshTokenManager

type RefreshTokenManager = core.RefreshTokenManager

Core types | 核心类型

func NewRefreshTokenManager

func NewRefreshTokenManager(storage Storage, prefix string, cfg *Config) *RefreshTokenManager

NewRefreshTokenManager creates a new refresh token manager | 创建新的刷新令牌管理器

type RequestContext

type RequestContext = core.RequestContext

Adapter interfaces | 适配器接口

type SaTokenContext

type SaTokenContext = core.SaTokenContext

Core types | 核心类型

func NewContext

func NewContext(ctx RequestContext, mgr *Manager) *SaTokenContext

NewContext creates a new Sa-Token context | 创建新的Sa-Token上下文

type Session

type Session = core.Session

Core types | 核心类型

func GetSession

func GetSession(loginID interface{}) (*Session, error)

GetSession gets the session for a login ID | 获取登录ID的Session

func GetSessionByToken

func GetSessionByToken(tokenValue string) (*Session, error)

GetSessionByToken gets the session by token | 根据Token获取Session

func GetTokenSession

func GetTokenSession(tokenValue string) (*Session, error)

GetTokenSession gets the token session | 获取Token的Session

func LoadSession

func LoadSession(id string, storage Storage, prefix string) (*Session, error)

LoadSession loads an existing session | 加载已存在的Session

func NewSession

func NewSession(id string, storage Storage, prefix string) *Session

NewSession creates a new session | 创建新的Session

type Storage

type Storage = core.Storage

Adapter interfaces | 适配器接口

type StorageProvider

type StorageProvider interface {
	Provide() Storage
}

StorageProvider 定义默认存储提供者接口,方便用户自定义存储实现

type StorageProviderFunc

type StorageProviderFunc func() Storage

StorageProviderFunc 简化 StorageProvider 的自定义方式

func (StorageProviderFunc) Provide

func (f StorageProviderFunc) Provide() Storage

Provide 实现 StorageProvider 接口

type TokenGenerator

type TokenGenerator = core.TokenGenerator

Core types | 核心类型

func NewTokenGenerator

func NewTokenGenerator(cfg *Config) *TokenGenerator

NewTokenGenerator creates a new token generator | 创建新的Token生成器

type TokenInfo

type TokenInfo = core.TokenInfo

Core types | 核心类型

func GetTokenInfo

func GetTokenInfo(tokenValue string) (*TokenInfo, error)

GetTokenInfo gets token information | 获取Token信息

type TokenStyle

type TokenStyle = core.TokenStyle

Configuration related types | 配置相关类型

Jump to

Keyboard shortcuts

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