Skip to main content

SaTokenManager

Struct SaTokenManager 

Source
pub struct SaTokenManager {
    pub config: Arc<SaTokenConfig>,
    /* private fields */
}
Expand description

sa-token 管理器:对外 API 门面,业务逻辑委托给 service / repository 层。

The sa-token manager: a thin facade delegating to the service and repository layers.

Fields§

§config: Arc<SaTokenConfig>

对外兼容:manager.config.token_name 经 Deref 仍可用。 Public field kept for compatibility; Arc derefs to SaTokenConfig. 构建期只包装一次,Clone Manager 只加引用计数,不再深拷贝配置。 Wrapped once at construction; cloning the manager is a refcount bump.

Implementations§

Source§

impl SaTokenManager

Source

pub async fn disable_level_with_type( &self, login_type: &str, login_id: &str, service: &str, level: i32, time: i64, ) -> Result<(), SaTokenError>

Disable at a level for a login type | 按登录类型分级禁用

Source

pub async fn disable_with_type( &self, login_type: &str, login_id: &str, time: i64, ) -> Result<(), SaTokenError>

Disable for a login type | 按登录类型禁用

Source

pub async fn get_disable_level_with_type( &self, login_type: &str, login_id: &str, service: &str, ) -> Result<i32, SaTokenError>

Read disable level for a login type | 按登录类型读取禁用等级

Source

pub async fn is_disable_level_with_type( &self, login_type: &str, login_id: &str, service: &str, level: i32, ) -> Result<bool, SaTokenError>

Whether disabled at/above level | 是否达到指定禁用等级

Source

pub async fn check_disable_level_with_type( &self, login_type: &str, login_id: &str, service: &str, level: i32, ) -> Result<(), SaTokenError>

Fail if disabled at/above level | 达到禁用等级则报错

Source

pub async fn check_disable_services_with_type( &self, login_type: &str, login_id: &str, services: &[&str], level: i32, ) -> Result<(), SaTokenError>

Fail if any listed service is disabled | 任一服务被禁用则报错

Source

pub async fn untie_disable_with_type( &self, login_type: &str, login_id: &str, service: &str, ) -> Result<(), SaTokenError>

Clear disable for a login type | 按登录类型解除禁用

Source

pub async fn disable_level( &self, login_id: &str, service: &str, level: i32, time: i64, ) -> Result<(), SaTokenError>

Disable at a level (default login type) | 分级禁用(默认登录类型)

Source

pub async fn disable( &self, login_id: &str, time: i64, ) -> Result<(), SaTokenError>

Disable account/service | 禁用账号或服务

Source

pub async fn get_disable_level( &self, login_id: &str, service: &str, ) -> Result<i32, SaTokenError>

Read disable level | 读取禁用等级

Source

pub async fn is_disable_level( &self, login_id: &str, service: &str, level: i32, ) -> Result<bool, SaTokenError>

Whether disabled at/above level | 是否达到指定禁用等级

Source

pub async fn check_disable_level( &self, login_id: &str, service: &str, level: i32, ) -> Result<(), SaTokenError>

Fail if disabled at/above level | 达到禁用等级则报错

Source

pub async fn check_disable_services( &self, login_id: &str, services: &[&str], level: i32, ) -> Result<(), SaTokenError>

Fail if any listed service is disabled | 任一服务被禁用则报错

Source

pub async fn untie_disable( &self, login_id: &str, service: &str, ) -> Result<(), SaTokenError>

Clear disable flag | 解除禁用

Source§

impl SaTokenManager

Source

pub async fn open_safe( &self, token: &TokenValue, service: &str, safe_time: i64, ) -> Result<(), SaTokenError>

为指定 token 开启二级认证

Source

pub async fn is_safe( &self, token: &TokenValue, service: &str, ) -> Result<bool, SaTokenError>

判断 token 是否已通过指定业务的二级认证

Source

pub async fn check_safe( &self, token: &TokenValue, service: &str, ) -> Result<(), SaTokenError>

校验二级认证;未通过抛出 SaTokenError::NotSafe

Source

pub async fn close_safe( &self, token: &TokenValue, service: &str, ) -> Result<(), SaTokenError>

关闭二级认证

Source

pub async fn get_safe_time( &self, token: &TokenValue, service: &str, ) -> Result<Option<i64>, SaTokenError>

获取二级认证剩余有效时间(秒);未认证返回 None

Source§

impl SaTokenManager

Source

pub async fn get_token_session( &self, token: &TokenValue, ) -> Result<SaSession, SaTokenError>

获取 Token-Session(不存在时按配置决定是否落盘创建)

Source

pub async fn get_anon_token_session( &self, token: &TokenValue, ) -> Result<SaSession, SaTokenError>

匿名 Token-Session:跳过登录校验,且从不自动落盘

Source

pub async fn save_token_session( &self, token: &TokenValue, session: &SaSession, ) -> Result<(), SaTokenError>

保存 Token-Session(TTL 与 token 生命周期对齐)

Source

pub async fn delete_token_session( &self, token: &TokenValue, ) -> Result<(), SaTokenError>

删除 Token-Session

Source§

impl SaTokenManager

Source

pub fn new(storage: Arc<dyn SaStorage>, config: SaTokenConfig) -> SaTokenManager

创建管理器实例。

配置只包装一次进 Arc,此后各层共享,Clone Manager 不再深拷贝配置。 Config is wrapped once into Arc; cloning the manager no longer deep-copies it.

Source

pub fn with_key_layout(self, layout: SaKeyLayout) -> SaTokenManager

运行时替换存储键布局(主要用于测试与迁移工具)

Source

pub fn with_serializer(self, serializer: SharedSerializer) -> SaTokenManager

替换序列化器(如启用 fory)。

显式复用原 event_bus,避免丢弃已注册的监听器。

Source

pub fn with_stp_interface(self, iface: Arc<dyn StpInterface>) -> SaTokenManager

注册权限/角色数据源 | Register the permission/role data source

Source

pub fn with_permission_matcher( self, matcher: Arc<dyn PermissionMatcher>, ) -> SaTokenManager

Replace the permission matcher used by AuthzService. 替换 AuthzService 使用的权限匹配器。

Source

pub fn with_role_matcher( self, matcher: Arc<dyn PermissionMatcher>, ) -> SaTokenManager

替换角色匹配策略(默认按 config.role_wildcard 选择精确/分段匹配)。 Replaces the role matching strategy (default follows config.role_wildcard).

Source

pub fn with_online_manager(self, manager: Arc<OnlineManager>) -> SaTokenManager

注册在线用户管理器 | Register the online user manager

Source

pub fn with_distributed_manager( self, manager: Arc<DistributedSessionManager>, ) -> SaTokenManager

注册分布式 Session 管理器 | Register the distributed session manager

Source

pub fn with_distributed_online(self) -> SaTokenManager

Attach a Dao-backed online manager (cross-instance presence). 挂上基于 Dao 的在线管理器(跨实例 presence)。

Source

pub fn start_background_cleanup( &self, config: CleanupConfig, ) -> BackgroundCleanupTask

Start optional background cleanup (disabled unless CleanupConfig.enabled). 启动可选后台清理(除非 CleanupConfig.enabled 否则不跑)。

Source

pub fn with_event_bus(self, event_bus: SaTokenEventBus) -> SaTokenManager

注入共享事件总线(支持多 Manager 共享 / 测试 mock)

Injects a shared event bus (supports multi-Manager sharing / test mocking).

§示例 | Example
ⓘ
let shared_bus = SaTokenEventBus::with_config(EventBusConfig {
    dispatch_mode: DispatchMode::Detached,
    listener_timeout: Some(Duration::from_secs(10)),
});
let mgr1 = SaTokenManager::new(storage1, config1).with_event_bus(shared_bus.clone());
let mgr2 = SaTokenManager::new(storage2, config2).with_event_bus(shared_bus.clone());
shared_bus.register(Arc::new(MyListener));
Source

pub fn keys(&self) -> &SaKeys

存储键构造器(A3 契约:返回引用,避免热路径克隆)

Source

pub fn storage(&self) -> &Arc<dyn SaStorage> ⓘ

底层存储 | Underlying storage

Source

pub fn dao(&self) -> &Arc<SaTokenDao> ⓘ

存储访问层 | Storage access layer

Source

pub fn serializer(&self) -> &SharedSerializer

当前序列化器 | Current serializer

Source

pub fn auth_service(&self) -> &Arc<AuthService> ⓘ

认证服务 | Authentication service

Source

pub fn token_repo(&self) -> &Arc<TokenRepo> ⓘ

Token 仓储 | Token repository

Source

pub fn session_repo(&self) -> &Arc<SessionRepo> ⓘ

Session 仓储 | Session repository

Source

pub fn authz_service(&self) -> &Arc<AuthzService> ⓘ

授权服务:权限/角色的读写与校验入口 | Authorization service

Source

pub fn grant_repo(&self) -> &Arc<GrantRepo> ⓘ

👎Deprecated since 0.2.0:

Use SaTokenManager::authz_service() so cache invalidation and StpInterface precedence are honoured

授权仓储(纯存储,绕过数据源优先级与缓存失效)。 请改用 authz_service()。

The storage-only grant repository, bypassing data-source precedence and cache invalidation. Use authz_service() instead.

Source

pub fn event_bus(&self) -> &SaTokenEventBus

事件总线 | Event bus

Source

pub fn online_manager(&self) -> Option<&Arc<OnlineManager>>

在线用户管理器 | Online user manager

Source

pub fn distributed_manager(&self) -> Option<&Arc<DistributedSessionManager>>

分布式 Session 管理器 | Distributed session manager

Source

pub async fn login( &self, login_id: impl Into<String>, ) -> Result<TokenValue, SaTokenError>

登录:为指定账号创建 token | Log in and issue a token

Source

pub async fn login_with_options( &self, login_id: impl Into<String>, login_type: Option<String>, device: Option<String>, extra_data: Option<Value>, nonce: Option<String>, expire_time: Option<DateTime<Utc>>, ) -> Result<TokenValue, SaTokenError>

登录(完整可选参数)。签名保持不变以维持对外兼容, 内部转换为 LoginRequest 后委托 AuthService。

Source

pub async fn login_with_token_info( &self, token_info: TokenInfo, ) -> Result<TokenValue, SaTokenError>

登录:使用完整 TokenInfo(SSO / 自定义 token 场景)。

Source

pub async fn logout(&self, token: &TokenValue) -> Result<(), SaTokenError>

登出指定 token | Log out a token

Source

pub async fn kick_out_by_token( &self, token: &TokenValue, ) -> Result<(), SaTokenError>

踢下线指定 token(标记 -5)| Kick out a token, marker -5

Source

pub async fn replaced_by_token( &self, token: &TokenValue, ) -> Result<(), SaTokenError>

顶下线指定 token(标记 -4)| Replace a token, marker -4

Source

pub async fn logout_by_login_id( &self, login_type: &str, login_id: &str, ) -> Result<(), SaTokenError>

按账号登出全部 token | Log out every token of an account

Source

pub async fn kick_out( &self, login_type: &str, login_id: &str, ) -> Result<(), SaTokenError>

按账号踢下线全部 token | Kick out every token of an account

Source

pub async fn get_token_info( &self, token: &TokenValue, ) -> Result<TokenInfo, SaTokenError>

读取并校验 token | Read and validate a token

Source

pub async fn get_token_by_login_id( &self, login_type: &str, login_id: &str, ) -> Result<TokenValue, SaTokenError>

按 login_type + login_id 读取当前映射 token Read the mapped token for login_type + login_id

Source

pub async fn get_all_tokens_by_login_id( &self, login_type: &str, login_id: &str, ) -> Result<Vec<TokenValue>, SaTokenError>

列出在线 token(B1 list 原语) List online tokens (B1 list primitive)

Source

pub async fn update_extra_data( &self, token: &TokenValue, extra_data: Value, ) -> Result<(), SaTokenError>

更新 extra_data 并经 TokenRepo 落盘 Update extra_data and persist via TokenRepo

Source

pub async fn update_active_timeout( &self, token: &TokenValue, seconds: i64, ) -> Result<(), SaTokenError>

Set per-token idle timeout. Errors unless dynamic_active_timeout is on. 设置单 token 闲置超时。未开启 dynamic_active_timeout 时返回 ConfigError。

Source

pub fn stp_logic(&self, login_type: &str) -> SaLogic

创建绑定 login_type 的廉价 Clone 门面 Create a cheap Clone facade bound to login_type

Source

pub async fn is_valid(&self, token: &TokenValue) -> bool

token 是否有效 | Whether the token is valid

Source

pub async fn renew_timeout( &self, token: &TokenValue, timeout_seconds: i64, ) -> Result<(), SaTokenError>

续期 token 到指定秒数 | Renew a token to an explicit lifetime

Source

pub async fn get_session( &self, login_id: &str, ) -> Result<SaSession, SaTokenError>

读取账号 Session(默认 login_type)

Source

pub async fn get_session_with_type( &self, login_type: &str, login_id: &str, ) -> Result<SaSession, SaTokenError>

读取账号 Session(指定 login_type,A3 契约)

Source

pub async fn save_session( &self, session: &SaSession, ) -> Result<(), SaTokenError>

保存账号 Session(修 B1-9:以 session.id 自身作为命名空间回写)

Source

pub async fn save_session_with_type( &self, login_type: &str, login_id: &str, session: &SaSession, ) -> Result<(), SaTokenError>

保存账号 Session(指定 login_type)

Source

pub async fn delete_session(&self, login_id: &str) -> Result<(), SaTokenError>

删除账号 Session(默认 login_type)

Source

pub async fn delete_session_with_type( &self, login_type: &str, login_id: &str, ) -> Result<(), SaTokenError>

删除账号 Session(指定 login_type)

Source

pub async fn get_terminal_list( &self, login_type: &str, login_id: &str, device_type: Option<&str>, ) -> Result<Vec<SaTerminalInfo>, SaTokenError>

获取指定账号的终端列表 | Terminal list of an account

Source

pub async fn get_token_value_list_by_login_id( &self, login_type: &str, login_id: &str, device_type: Option<&str>, ) -> Result<Vec<String>, SaTokenError>

获取指定账号的 token 列表(来自终端列表)

Source

pub async fn get_terminal_info_by_token( &self, token: &TokenValue, ) -> Result<Option<SaTerminalInfo>, SaTokenError>

按 token 反查终端信息 | Look up terminal info by token

Source

pub async fn get_permissions_with_type( &self, login_type: &str, login_id: &str, ) -> Result<Vec<String>, SaTokenError>

获取权限列表(指定账号体系)| Permission list for a login type

Source

pub async fn set_permissions_with_type( &self, login_type: &str, login_id: &str, permissions: Vec<String>, ) -> Result<(), SaTokenError>

覆盖权限列表(指定账号体系)| Overwrite the permission list for a login type

Source

pub async fn add_permission_with_type( &self, login_type: &str, login_id: &str, permission: String, ) -> Result<(), SaTokenError>

追加单个权限(指定账号体系,B2-35 新增) Append one permission for a login type (added in B2-35).

Source

pub async fn remove_permission_with_type( &self, login_type: &str, login_id: &str, permission: &str, ) -> Result<(), SaTokenError>

移除单个权限(指定账号体系,B2-35 新增) Remove one permission for a login type (added in B2-35).

Source

pub async fn clear_permissions_with_type( &self, login_type: &str, login_id: &str, ) -> Result<(), SaTokenError>

清空权限(指定账号体系,B2-35 新增) Clear permissions for a login type (added in B2-35).

Source

pub async fn get_roles_with_type( &self, login_type: &str, login_id: &str, ) -> Result<Vec<String>, SaTokenError>

获取角色列表(指定账号体系)| Role list for a login type

Source

pub async fn set_roles_with_type( &self, login_type: &str, login_id: &str, roles: Vec<String>, ) -> Result<(), SaTokenError>

覆盖角色列表(指定账号体系)| Overwrite the role list for a login type

Source

pub async fn add_role_with_type( &self, login_type: &str, login_id: &str, role: String, ) -> Result<(), SaTokenError>

追加单个角色(指定账号体系,B2-35 新增) Append one role for a login type (added in B2-35).

Source

pub async fn remove_role_with_type( &self, login_type: &str, login_id: &str, role: &str, ) -> Result<(), SaTokenError>

移除单个角色(指定账号体系,B2-35 新增) Remove one role for a login type (added in B2-35).

Source

pub async fn clear_roles_with_type( &self, login_type: &str, login_id: &str, ) -> Result<(), SaTokenError>

清空角色(指定账号体系,B2-35 新增) Clear roles for a login type (added in B2-35).

Source

pub async fn get_permissions( &self, login_id: &str, ) -> Result<Vec<String>, SaTokenError>

获取权限列表 | Permission list

Source

pub async fn set_permissions( &self, login_id: &str, permissions: Vec<String>, ) -> Result<(), SaTokenError>

覆盖权限列表 | Overwrite the permission list

Source

pub async fn add_permission( &self, login_id: &str, permission: String, ) -> Result<(), SaTokenError>

追加单个权限 | Append one permission

Source

pub async fn remove_permission( &self, login_id: &str, permission: &str, ) -> Result<(), SaTokenError>

移除单个权限 | Remove one permission

Source

pub async fn clear_permissions( &self, login_id: &str, ) -> Result<(), SaTokenError>

清空权限 | Clear all permissions

Source

pub async fn get_roles( &self, login_id: &str, ) -> Result<Vec<String>, SaTokenError>

获取角色列表 | Role list

Source

pub async fn set_roles( &self, login_id: &str, roles: Vec<String>, ) -> Result<(), SaTokenError>

覆盖角色列表 | Overwrite the role list

Source

pub async fn add_role( &self, login_id: &str, role: String, ) -> Result<(), SaTokenError>

追加单个角色 | Append one role

Source

pub async fn remove_role( &self, login_id: &str, role: &str, ) -> Result<(), SaTokenError>

移除单个角色 | Remove one role

Source

pub async fn clear_roles(&self, login_id: &str) -> Result<(), SaTokenError>

清空角色 | Clear all roles

Trait Implementations§

Source§

impl Clone for SaTokenManager

Source§

fn clone(&self) -> SaTokenManager

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SaTokenManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more