Skip to main content

StpUtil

Struct StpUtil 

Source
pub struct StpUtil;
Expand description

Static helpers for login, logout, and authorization. 登录、登出与鉴权的静态辅助方法。

Implementations§

Source§

impl StpUtil

Source

pub fn try_init_manager(manager: SaTokenManager) -> SaTokenResult<()>

尝试初始化全局 Manager(应用启动调用一次) Try to initialize the global manager (call once at startup).

重复调用返回 AlreadyInitialized,不 panic。 Duplicate calls return AlreadyInitialized without panicking.

Source

pub fn init_manager(manager: SaTokenManager)

👎Deprecated:

use try_init_manager() which returns Result instead of panicking

初始化全局 Manager(兼容旧 API;重复仍 panic) Initialize global manager (legacy; still panics on duplicate).

§示例
ⓘ
let manager = SaTokenConfig::builder()
    .storage(Arc::new(MemoryStorage::new()))
    .build();
StpUtil::init_manager(manager);
Source

pub fn try_get_manager() -> SaTokenResult<&'static Arc<SaTokenManager>>

尝试获取全局 Manager Try to get the global manager

Source

pub fn event_bus() -> Option<&'static SaTokenEventBus>

获取事件总线,用于注册监听器

§示例
ⓘ
use sa_token_core::{StpUtil, SaTokenListener};
use async_trait::async_trait;

struct MyListener;

#[async_trait]
impl SaTokenListener for MyListener {
    async fn on_login(&self, login_id: &str, token: &str, login_type: &str) {
        println!("用户 {} 登录了", login_id);
    }
}

// 注册监听器
StpUtil::event_bus().register(Arc::new(MyListener));

尝试获取事件总线;未初始化返回 None(不 panic)。 Try to get the event bus; None before init (no panic).

Source

pub fn register_listener(listener: Arc<dyn SaTokenListener>)

注册事件监听器(便捷方法);未初始化时静默跳过。 Register a listener; no-op when the manager is not initialized.

§示例
ⓘ
StpUtil::register_listener(Arc::new(MyListener));
Source

pub async fn login(login_id: impl LoginId) -> SaTokenResult<TokenValue>

会话登录

§示例
ⓘ
// 支持字符串 ID
let token = StpUtil::login("user_123").await?;

// 支持数字 ID
let token = StpUtil::login(10001).await?;
let token = StpUtil::login(10001_i64).await?;
Source

pub async fn login_with_type( login_id: impl LoginId, login_type: impl Into<String>, ) -> SaTokenResult<TokenValue>

login_with_type — login with type | login_with_type

Source

pub async fn login_with_extra( login_id: impl LoginId, extra_data: Value, ) -> SaTokenResult<TokenValue>

登录并设置额外数据 | Login with extra data

§参数 | Arguments
  • login_id - 登录ID | Login ID
  • extra_data - 额外数据 | Extra data
Source

pub async fn login_with_manager( manager: &SaTokenManager, login_id: impl Into<String>, ) -> SaTokenResult<TokenValue>

会话登录(带 manager 参数的版本,向后兼容)

Source

pub async fn logout(token: &TokenValue) -> SaTokenResult<()>

会话登出

Source

pub async fn logout_with_manager( manager: &SaTokenManager, token: &TokenValue, ) -> SaTokenResult<()>

logout_with_manager — logout with manager | logout_with_manager

Opt-in write of the token cookie (no-op unless is_write_cookie is true). 可选写入 token Cookie(未开启 is_write_cookie 时为空操作)。

Clear the token cookie (same opt-in guard as write). 清除 token Cookie(与写入同一开关)。

Source

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

Set per-token idle timeout. No-op unless dynamic_active_timeout is enabled. 设置单 token 闲置超时。未打开 dynamic_active_timeout 时返回 ConfigError。

Source

pub async fn kick_out(login_id: impl LoginId) -> SaTokenResult<()>

踢人下线(使用当前请求 login_type,无上下文则 default) Kick out (uses current request login_type; falls back to default).

Source

pub async fn kick_out_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

kick_out_with_type — kick out with type | kick_out_with_type

Source

pub async fn kick_out_with_manager( manager: &SaTokenManager, login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

kick_out_with_manager — kick out with manager | kick_out_with_manager

Source

pub async fn logout_by_login_id(login_id: impl LoginId) -> SaTokenResult<()>

强制登出(使用当前请求 login_type,无上下文则 default) Force logout by login_id (uses current request login_type; falls back to default).

Source

pub async fn logout_by_login_id_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

logout_by_login_id_with_type — logout by login id with type | logout_by_login_id_with_type

Source

pub async fn logout_by_token(token: &TokenValue) -> SaTokenResult<()>

根据 token 登出(别名方法,更直观)

Source

pub fn get_token_value() -> SaTokenResult<TokenValue>

获取当前请求的 token(无参数,从上下文获取)

§示例
ⓘ
// 在请求处理函数中
let token = StpUtil::get_token_value()?;
Source

pub async fn logout_current() -> SaTokenResult<()>

当前会话登出(无参数,从上下文获取 token)

§示例
ⓘ
// 在请求处理函数中
StpUtil::logout_current().await?;
Source

pub fn is_login_current() -> bool

检查当前会话是否登录(同步弱校验:仅看上下文是否有 token 字符串,不查存储) Sync weak check: whether context has a token string (does not hit storage).

踢出/过期后若中间件未刷新上下文,仍可能为 true。强保证用 [check_login_current_async]。 May still be true after kick/expire if middleware did not refresh context.

Source

pub fn check_login_current() -> SaTokenResult<()>

检查当前会话登录状态(同步弱校验),未登录则抛出异常 Sync weak check; returns error when context has no token.

Source

pub async fn check_login_current_async() -> SaTokenResult<()>

异步强校验:上下文有 token 且 storage 仍有效 Async strong check: context has a token AND storage still considers it valid.

Source

pub async fn is_login_current_async() -> bool

异步强校验是否登录 Async strong login check returning bool.

Source

pub async fn get_login_id_as_string() -> SaTokenResult<String>

获取当前会话的 login_id(String 类型,无参数)

§示例
ⓘ
// 在请求处理函数中
let login_id = StpUtil::get_login_id_as_string().await?;
Source

pub async fn get_login_id_as_long() -> SaTokenResult<i64>

获取当前会话的 login_id(i64 类型,无参数)

§示例
ⓘ
// 在请求处理函数中
let user_id = StpUtil::get_login_id_as_long().await?;
Source

pub fn get_token_info_current() -> SaTokenResult<Arc<TokenInfo>>

获取当前会话的 token 信息(无参数)

§示例
ⓘ
// 在请求处理函数中
let token_info = StpUtil::get_token_info_current()?;
println!("Token 创建时间: {:?}", token_info.create_time);
Source

pub async fn is_login(token: &TokenValue) -> bool

检查当前 token 是否已登录

Source

pub async fn is_login_by_login_id(login_id: impl LoginId) -> bool

根据登录 ID 检查是否已登录

§示例
ⓘ
let is_logged_in = StpUtil::is_login_by_login_id("user_123").await;
let is_logged_in = StpUtil::is_login_by_login_id(10001).await;
Source

pub async fn is_login_with_manager( manager: &SaTokenManager, token: &TokenValue, ) -> bool

is_login_with_manager — is login with manager | is_login_with_manager

Source

pub async fn check_login(token: &TokenValue) -> SaTokenResult<()>

检查当前 token 是否已登录,如果未登录则抛出异常

Source

pub async fn get_token_info(token: &TokenValue) -> SaTokenResult<TokenInfo>

获取 token 信息

Source

pub async fn get_login_id(token: &TokenValue) -> SaTokenResult<String>

获取当前 token 的登录ID

Source

pub async fn get_login_id_or_default( token: &TokenValue, default: impl Into<String>, ) -> String

获取当前 token 的登录ID,如果未登录则返回默认值

Source

pub async fn get_token_by_login_id( login_id: impl LoginId, ) -> SaTokenResult<TokenValue>

根据登录 ID 获取当前用户的 token

§示例
ⓘ
let token = StpUtil::get_token_by_login_id("user_123").await?;
let token = StpUtil::get_token_by_login_id(10001).await?;
Source

pub async fn get_token_by_login_id_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<TokenValue>

指定 login_type 获取当前账号的 login:token 映射。

委托 Manager,禁止 StpUtil 直连 TokenRepo。

Source

pub async fn get_all_tokens_by_login_id( login_id: impl LoginId, ) -> SaTokenResult<Vec<TokenValue>>

get_all_tokens_by_login_id — get all tokens by login id | get_all_tokens_by_login_id

Source

pub async fn get_all_tokens_by_login_id_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<Vec<TokenValue>>

指定 login_type 获取全部在线 token。

委托 Manager(内部走 TokenRepo list 原语)。

Source

pub async fn get_session_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<SaSession>

get_session_with_type — get session with type | get_session_with_type

Source

pub async fn delete_session_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

delete_session_with_type — delete session with type | delete_session_with_type

Source

pub async fn get_session(login_id: impl LoginId) -> SaTokenResult<SaSession>

获取当前登录账号的 Session(使用当前请求 login_type) Get session for login_id (uses current request login_type).

Source

pub async fn save_session(session: &SaSession) -> SaTokenResult<()>

保存 Session

Source

pub async fn delete_session(login_id: impl LoginId) -> SaTokenResult<()>

删除 Session(使用当前请求 login_type) Delete session (uses current request login_type).

Source

pub async fn set_session_value<T: Serialize>( login_id: impl LoginId, key: &str, value: T, ) -> SaTokenResult<()>

在 Session 中设置值(使用当前请求 login_type) Set a value in session (uses current request login_type).

Source

pub async fn get_session_value<T: DeserializeOwned>( login_id: impl LoginId, key: &str, ) -> SaTokenResult<Option<T>>

从 Session 中获取值(使用当前请求 login_type) Get a value from session (uses current request login_type).

Source

pub fn create_token(token_value: impl Into<String>) -> TokenValue

创建一个新的 token(但不登录)

Source

pub fn is_valid_token_format(token: &str) -> bool

检查 token 格式是否有效(仅检查格式,不检查是否存在于存储中)

Source§

impl StpUtil

Source

pub async fn set_permissions_with_type( login_type: &str, login_id: impl LoginId, permissions: Vec<String>, ) -> SaTokenResult<()>

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

Source

pub async fn set_permissions( login_id: impl LoginId, permissions: Vec<String>, ) -> SaTokenResult<()>

覆盖权限列表 | Overwrite permissions

Source

pub async fn add_permission_with_type( login_type: &str, login_id: impl LoginId, permission: impl Into<String>, ) -> SaTokenResult<()>

追加单个权限(指定账号体系)| Append one permission for a login type

Source

pub async fn add_permission( login_id: impl LoginId, permission: impl Into<String>, ) -> SaTokenResult<()>

追加单个权限 | Append one permission

Source

pub async fn remove_permission_with_type( login_type: &str, login_id: impl LoginId, permission: &str, ) -> SaTokenResult<()>

移除单个权限(指定账号体系)| Remove one permission for a login type

Source

pub async fn remove_permission( login_id: impl LoginId, permission: &str, ) -> SaTokenResult<()>

移除单个权限 | Remove one permission

Source

pub async fn clear_permissions_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

清空权限(指定账号体系)| Clear permissions for a login type

Source

pub async fn clear_permissions(login_id: impl LoginId) -> SaTokenResult<()>

清空权限 | Clear permissions

Source

pub async fn try_get_permissions_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<Vec<String>>

获取权限列表(指定账号体系,错误上抛) Permission list for a login type, propagating errors.

Source

pub async fn try_get_permissions( login_id: impl LoginId, ) -> SaTokenResult<Vec<String>>

获取权限列表(错误上抛,修 B2-39) Unlike get_permissions, this propagates storage failures.

Source

pub async fn get_permissions(login_id: impl LoginId) -> Vec<String>

获取权限列表(失败返回空表 + 告警日志,修 B2-39) Keeps the old signature; failures now log a warning instead of being silent.

Source

pub async fn has_permission_with_type( login_type: &str, login_id: impl LoginId, permission: &str, ) -> bool

单个权限校验(指定账号体系)| Single permission check for a login type

Source

pub async fn has_permission(login_id: impl LoginId, permission: &str) -> bool

单个权限校验 | Single permission check

Source

pub async fn has_all_permissions_with_type( login_type: &str, login_id: impl LoginId, permissions: &[&str], ) -> bool

批量权限校验(AND,指定账号体系)| Batch AND check for a login type

Source

pub async fn has_all_permissions( login_id: impl LoginId, permissions: &[&str], ) -> bool

批量权限校验(AND)| Batch AND check

Source

pub async fn has_permissions_and( login_id: impl LoginId, permissions: &[&str], ) -> bool

has_all_permissions 的别名 | Alias

Source

pub async fn has_any_permission_with_type( login_type: &str, login_id: impl LoginId, permissions: &[&str], ) -> bool

批量权限校验(OR,指定账号体系)| Batch OR check for a login type

Source

pub async fn has_any_permission( login_id: impl LoginId, permissions: &[&str], ) -> bool

批量权限校验(OR)| Batch OR check

Source

pub async fn has_permissions_or( login_id: impl LoginId, permissions: &[&str], ) -> bool

has_any_permission 的别名 | Alias

Source

pub async fn check_permission_with_type( login_type: &str, login_id: impl LoginId, permission: &str, ) -> SaTokenResult<()>

权限校验(失败返回 Err,指定账号体系) Permission check returning Err on denial, for a login type.

Source

pub async fn check_permission( login_id: impl LoginId, permission: &str, ) -> SaTokenResult<()>

权限校验(失败返回 Err)| Permission check returning Err on denial

Source

pub async fn check_all_permissions( login_id: impl LoginId, permissions: &[&str], ) -> SaTokenResult<()>

批量权限校验(AND,失败返回 Err,B2-36 新增) Batch AND check returning Err on denial.

Source

pub async fn check_any_permission( login_id: impl LoginId, permissions: &[&str], ) -> SaTokenResult<()>

批量权限校验(OR,失败返回 Err,B2-36 新增) Batch OR check returning Err on denial.

Source§

impl StpUtil

Source

pub async fn set_roles_with_type( login_type: &str, login_id: impl LoginId, roles: Vec<String>, ) -> SaTokenResult<()>

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

Source

pub async fn set_roles( login_id: impl LoginId, roles: Vec<String>, ) -> SaTokenResult<()>

覆盖角色列表 | Overwrite roles

Source

pub async fn add_role_with_type( login_type: &str, login_id: impl LoginId, role: impl Into<String>, ) -> SaTokenResult<()>

追加单个角色(指定账号体系)| Append one role for a login type

Source

pub async fn add_role( login_id: impl LoginId, role: impl Into<String>, ) -> SaTokenResult<()>

追加单个角色 | Append one role

Source

pub async fn remove_role_with_type( login_type: &str, login_id: impl LoginId, role: &str, ) -> SaTokenResult<()>

移除单个角色(指定账号体系)| Remove one role for a login type

Source

pub async fn remove_role( login_id: impl LoginId, role: &str, ) -> SaTokenResult<()>

移除单个角色 | Remove one role

Source

pub async fn clear_roles_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<()>

清空角色(指定账号体系)| Clear roles for a login type

Source

pub async fn clear_roles(login_id: impl LoginId) -> SaTokenResult<()>

清空角色 | Clear roles

Source

pub async fn try_get_roles_with_type( login_type: &str, login_id: impl LoginId, ) -> SaTokenResult<Vec<String>>

获取角色列表(指定账号体系,错误上抛) Role list for a login type, propagating errors.

Source

pub async fn try_get_roles(login_id: impl LoginId) -> SaTokenResult<Vec<String>>

获取角色列表(错误上抛,修 B2-39)| Role list propagating errors

Source

pub async fn get_roles(login_id: impl LoginId) -> Vec<String>

获取角色列表(失败返回空表 + 告警日志)| Role list, empty on failure with a warning

Source

pub async fn has_role_with_type( login_type: &str, login_id: impl LoginId, role: &str, ) -> bool

单个角色校验(指定账号体系)| Single role check for a login type

Source

pub async fn has_role(login_id: impl LoginId, role: &str) -> bool

单个角色校验 | Single role check

Source

pub async fn has_all_roles_with_type( login_type: &str, login_id: impl LoginId, roles: &[&str], ) -> bool

批量角色校验(AND,指定账号体系)| Batch AND role check for a login type

Source

pub async fn has_all_roles(login_id: impl LoginId, roles: &[&str]) -> bool

批量角色校验(AND)| Batch AND role check

Source

pub async fn has_roles_and(login_id: impl LoginId, roles: &[&str]) -> bool

has_all_roles 的别名 | Alias

Source

pub async fn has_any_role_with_type( login_type: &str, login_id: impl LoginId, roles: &[&str], ) -> bool

批量角色校验(OR,指定账号体系)| Batch OR role check for a login type

Source

pub async fn has_any_role(login_id: impl LoginId, roles: &[&str]) -> bool

批量角色校验(OR)| Batch OR role check

Source

pub async fn has_roles_or(login_id: impl LoginId, roles: &[&str]) -> bool

has_any_role 的别名 | Alias

Source

pub async fn check_role_with_type( login_type: &str, login_id: impl LoginId, role: &str, ) -> SaTokenResult<()>

角色校验(失败返回 Err,指定账号体系) Role check returning Err on denial, for a login type.

Source

pub async fn check_role(login_id: impl LoginId, role: &str) -> SaTokenResult<()>

角色校验(失败返回 Err)| Role check returning Err on denial

Source

pub async fn check_all_roles( login_id: impl LoginId, roles: &[&str], ) -> SaTokenResult<()>

批量角色校验(AND,失败返回 Err,B2-36 新增) Batch AND role check returning Err.

Source

pub async fn check_any_role( login_id: impl LoginId, roles: &[&str], ) -> SaTokenResult<()>

批量角色校验(OR,失败返回 Err,B2-36 新增) Batch OR role check returning Err.

Source§

impl StpUtil

Source

pub async fn disable(login_id: impl LoginId, time: i64) -> SaTokenResult<()>

封禁账号(默认服务 login;使用当前请求 login_type) Disable account (default service; uses current request login_type).

Source

pub async fn disable_with_type( login_type: &str, login_id: impl LoginId, time: i64, ) -> SaTokenResult<()>

指定 login_type 封禁 Disable with explicit login_type.

Source

pub async fn disable_level( login_id: impl LoginId, service: &str, level: i32, time: i64, ) -> SaTokenResult<()>

封禁账号指定服务与等级(使用当前请求 login_type) Disable with service/level (uses current request login_type).

Source

pub async fn check_disable(login_id: impl LoginId) -> SaTokenResult<()>

校验封禁(默认服务 login、最低等级)

Source

pub async fn check_disable_service( login_id: impl LoginId, service: &str, ) -> SaTokenResult<()>

校验指定服务的封禁

Source

pub async fn check_disable_services( login_id: impl LoginId, services: &[&str], ) -> SaTokenResult<()>

校验多个服务的封禁(使用当前请求 login_type)

Source

pub async fn check_disable_level( login_id: impl LoginId, service: &str, level: i32, ) -> SaTokenResult<()>

校验封禁等级(使用当前请求 login_type)

Source

pub async fn get_disable_level( login_id: impl LoginId, service: &str, ) -> SaTokenResult<i32>

获取封禁等级(使用当前请求 login_type) Get disable level (uses current request login_type).

Source

pub async fn get_disable_level_with_type( login_type: &str, login_id: impl LoginId, service: &str, ) -> SaTokenResult<i32>

指定 login_type 获取封禁等级 Get disable level with explicit login_type.

Source

pub async fn untie_disable( login_id: impl LoginId, service: &str, ) -> SaTokenResult<()>

解封(使用当前请求 login_type)

Source§

impl StpUtil

Source

pub async fn open_safe(service: &str, safe_time: i64) -> SaTokenResult<()>

为当前 token 开启二级认证

Source

pub async fn is_safe(service: &str) -> SaTokenResult<bool>

当前 token 是否已通过二级认证

Source

pub async fn check_safe(service: &str) -> SaTokenResult<()>

校验当前 token 的二级认证

Source

pub async fn close_safe(service: &str) -> SaTokenResult<()>

关闭当前 token 的二级认证

Source§

impl StpUtil

Source

pub fn switch_to(login_id: impl LoginId)

临时切换为指定 login_id(写入请求上下文,task-local 与 thread-local 单轨就地突变)

Temporarily switch to the specified login_id (in-place mutation across task-local and thread-local).

Source

pub fn end_switch()

结束临时身份切换(清除 switch_login_id,恢复真实身份)

End identity switch (clears switch_login_id, restoring real identity).

Source

pub fn is_switch() -> bool

是否处于临时身份切换中

Whether currently inside an identity switch.

Source

pub fn get_switch_login_id() -> Option<String>

获取临时切换的 login_id(审计日志用)

Get the switched login_id (for audit logs).

Source§

impl StpUtil

Source

pub async fn kick_out_batch<T: LoginId>( login_ids: &[T], ) -> SaTokenResult<Vec<Result<(), SaTokenError>>>

批量踢人下线(使用当前请求 login_type) Batch kick-out (uses current request login_type).

Source

pub async fn get_token_timeout(token: &TokenValue) -> SaTokenResult<Option<i64>>

获取 token 剩余有效时间(秒)

Source

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

续期 token(重置过期时间)。

委托 Manager → AuthService,避免 StpUtil 直写 storage 与 B1 续签策略分叉。

Source

pub async fn set_extra_data( token: &TokenValue, extra_data: Value, ) -> SaTokenResult<()>

设置 Token 的额外数据。

委托 Manager::update_extra_data,禁止 StpUtil 直连 TokenRepo。

Source

pub async fn get_extra_data(token: &TokenValue) -> SaTokenResult<Option<Value>>

获取 Token 的额外数据 | Get extra data from token

§参数 | Arguments
  • token - Token值 | Token value
Source

pub async fn get_terminal_list( login_id: &str, device_type: Option<&str>, ) -> SaTokenResult<Vec<SaTerminalInfo>>

List terminals for the account | 列出账号终端

Source

pub async fn get_token_value_list_by_login_id( login_id: &str, device_type: Option<&str>, ) -> SaTokenResult<Vec<String>>

get_token_value_list_by_login_id — get token value list by login id | get_token_value_list_by_login_id

Source

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

Terminal info for a token | 按 Token 查终端信息

Source

pub async fn check_current_terminal(expected: &str) -> SaTokenResult<()>

Require the current token’s device type to equal expected (exact match). 要求当前 token 的设备类型等于 expected(精确匹配,区分大小写)。

Source

pub fn stp_logic(login_type: &str) -> SaTokenResult<SaLogic>

创建绑定 login_type 的廉价 Clone 门面(无全局注册表) Create a cheap Clone facade for login_type (no global registry).

Source

pub fn put_stp_logic(_logic: SaLogic)

👎Deprecated:

SaLogic is a cloneable facade; use SaLogic::new / StpUtil::stp_logic

已废弃:SaLogic 为可克隆门面,无需注册 Deprecated: SaLogic is a cloneable facade; nothing to register.

Source

pub fn remove_stp_logic(_login_type: &str)

👎Deprecated:

SaLogic is a cloneable facade; nothing to remove

已废弃:SaLogic 为可克隆门面,无需移除 Deprecated: SaLogic is a cloneable facade; nothing to remove.

Source

pub async fn get_token_session(token: &TokenValue) -> SaTokenResult<SaSession>

获取 token-session

Source

pub async fn get_token_session_current() -> SaTokenResult<SaSession>

获取当前请求的 token-session

Source

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

保存 token-session

Source

pub async fn delete_token_session(token: &TokenValue) -> SaTokenResult<()>

删除 token-session

Source

pub async fn kick_out_by_token(token: &TokenValue) -> SaTokenResult<()>

按 token 踢人下线

Source

pub async fn with_grant_scope<F, T>(future: F) -> T
where F: Future<Output = T>,

在一段异步逻辑内启用「授权快照」:期间同一账号的权限/角色只读一次。 Enables a per-scope authorization snapshot inside an async block.

Source

pub async fn check_permission_or_role( login_id: impl LoginId, permissions: &[&str], roles: &[&str], ) -> SaTokenResult<()>

组合校验:权限集合 ∪ 角色集合中任一命中即通过(供 #[sa_check_or] 宏使用)。 Combined check: passes when any of the permissions or roles matches.

Source

pub fn builder(login_id: impl LoginId) -> TokenBuilder

创建 Token 构建器,用于链式调用 | Create token builder for chain calls

§示例 | Example
ⓘ
use serde_json::json;

// 链式调用示例
let token = StpUtil::builder("user_123")
    .extra_data(json!({"ip": "192.168.1.1"}))
    .device("pc")
    .login_type("admin")
    .login()
    .await?;
Source

pub fn request_sign() -> SaTokenResult<RequestSign>

Build a signer from config (sign_secret_key). Errors if the secret is missing. 用配置中的 sign_secret_key 构造签名器;密钥缺失则报错。

Source

pub async fn sign_params( params: BTreeMap<String, String>, ) -> SaTokenResult<BTreeMap<String, String>>

Create signed params (timestamp + nonce + sign). 创建已签名参数(timestamp + nonce + sign)。

Source

pub async fn check_sign(params: &BTreeMap<String, String>) -> SaTokenResult<()>

Verify request signature from the sign field. 校验请求中 sign 字段的签名。

Source

pub async fn get_same_token() -> SaTokenResult<String>

Get current Same-Token (create if missing). 获取当前 Same-Token(不存在则创建)。

Source

pub async fn refresh_same_token() -> SaTokenResult<String>

Refresh Same-Token. 刷新 Same-Token。

Source

pub async fn check_same_token(token: &str) -> SaTokenResult<()>

Check a Same-Token value. 校验 Same-Token 值。

Source

pub async fn create_temp_token( value: impl Into<String>, timeout_secs: i64, ) -> SaTokenResult<String>

Create a short-lived temp token in the default namespace. 在默认命名空间创建短时临时令牌。

Source

pub async fn parse_temp_token(token: &str) -> SaTokenResult<TempTokenRecord>

Parse a temp token from the default namespace. 解析默认命名空间中的临时令牌。

Source

pub async fn delete_temp_token(token: &str) -> SaTokenResult<()>

Delete a temp token from the default namespace. 删除默认命名空间中的临时令牌。

Trait Implementations§

Source§

impl Debug for StpUtil

Source§

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

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> ForyObject for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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