WsAuthManager

Struct WsAuthManager 

Source
pub struct WsAuthManager { /* private fields */ }
Expand description

WebSocket authentication manager WebSocket 认证管理器

Provides authentication and verification for WebSocket connections 为 WebSocket 连接提供认证和验证功能

Implementations§

Source§

impl WsAuthManager

Source

pub fn new(manager: Arc<SaTokenManager>) -> Self

Create a new WebSocket authentication manager with default extractor 使用默认提取器创建新的 WebSocket 认证管理器

§Arguments | 参数
  • manager - SaToken manager instance | SaToken 管理器实例
§Example | 示例
let ws_auth = WsAuthManager::new(manager);
Source

pub fn with_extractor( manager: Arc<SaTokenManager>, extractor: Arc<dyn WsTokenExtractor>, ) -> Self

Create a new WebSocket authentication manager with custom extractor 使用自定义提取器创建新的 WebSocket 认证管理器

§Arguments | 参数
  • manager - SaToken manager instance | SaToken 管理器实例
  • extractor - Custom token extractor | 自定义 Token 提取器
§Example | 示例
let custom_extractor = Arc::new(MyCustomExtractor);
let ws_auth = WsAuthManager::with_extractor(manager, custom_extractor);
Source

pub async fn authenticate( &self, headers: &HashMap<String, String>, query: &HashMap<String, String>, ) -> Result<WsAuthInfo, SaTokenError>

Authenticate a WebSocket connection 认证 WebSocket 连接

This method will trigger a Login event after successful authentication 此方法在认证成功后会触发 Login 事件

§Arguments | 参数
  • headers - HTTP headers from the WebSocket handshake | WebSocket 握手的 HTTP 请求头
  • query - Query parameters from the connection URL | 连接 URL 的查询参数
§Returns | 返回值
  • Ok(WsAuthInfo) - Authentication successful | 认证成功
  • Err(SaTokenError) - Authentication failed | 认证失败
§Errors | 错误
  • NotLogin - No token found | 未找到 Token
  • TokenNotFound - Token not found in storage | 存储中未找到 Token
  • TokenExpired - Token has expired | Token 已过期
§Events | 事件

Publishes SaTokenEvent::Login with login_type = “websocket” 发布 SaTokenEvent::Login 事件,login_type = “websocket”

§Example | 示例
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer token123".to_string());
 
let auth_info = ws_auth.authenticate(&headers, &HashMap::new()).await?;
println!("User {} connected", auth_info.login_id);
 
// Event listeners will be notified of WebSocket authentication
// 事件监听器将收到 WebSocket 认证通知
Source

pub async fn verify_token(&self, token: &str) -> Result<String, SaTokenError>

Verify a token and return the login ID 验证 Token 并返回登录 ID

§Arguments | 参数
  • token - Token string to verify | 要验证的 Token 字符串
§Returns | 返回值
  • Ok(login_id) - Token is valid | Token 有效
  • Err(SaTokenError) - Token is invalid or expired | Token 无效或已过期
§Example | 示例
let login_id = ws_auth.verify_token("token123").await?;
println!("Token belongs to user: {}", login_id);
Source

pub async fn refresh_ws_session( &self, auth_info: &WsAuthInfo, ) -> Result<(), SaTokenError>

Refresh a WebSocket session by verifying its token 通过验证 Token 刷新 WebSocket 会话

§Arguments | 参数
  • auth_info - WebSocket authentication info | WebSocket 认证信息
§Returns | 返回值
  • Ok(()) - Session refreshed successfully | 会话刷新成功
  • Err(SaTokenError) - Token is invalid or expired | Token 无效或已过期
§Example | 示例
ws_auth.refresh_ws_session(&auth_info).await?;

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

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

§

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> Same for T

Source§

type Output = T

Should always be Self
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
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