pub struct WsAuthManager { /* private fields */ }Expand description
WebSocket authentication manager WebSocket 认证管理器
Provides authentication and verification for WebSocket connections 为 WebSocket 连接提供认证和验证功能
Implementations§
Source§impl WsAuthManager
impl WsAuthManager
Sourcepub fn new(manager: Arc<SaTokenManager>) -> WsAuthManager
pub fn new(manager: Arc<SaTokenManager>) -> WsAuthManager
Sourcepub fn with_extractor(
manager: Arc<SaTokenManager>,
extractor: Arc<dyn WsTokenExtractor>,
) -> WsAuthManager
pub fn with_extractor( manager: Arc<SaTokenManager>, extractor: Arc<dyn WsTokenExtractor>, ) -> WsAuthManager
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);Sourcepub async fn authenticate(
&self,
headers: &HashMap<String, String>,
query: &HashMap<String, String>,
) -> Result<WsAuthInfo, SaTokenError>
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 | 未找到 TokenTokenNotFound- Token not found in storage | 存储中未找到 TokenTokenExpired- 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 认证通知Sourcepub async fn verify_token(&self, token: &str) -> Result<String, SaTokenError>
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);Sourcepub async fn refresh_ws_session(
&self,
auth_info: &WsAuthInfo,
) -> Result<(), SaTokenError>
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§
impl Freeze for WsAuthManager
impl !RefUnwindSafe for WsAuthManager
impl Send for WsAuthManager
impl Sync for WsAuthManager
impl Unpin for WsAuthManager
impl !UnwindSafe for WsAuthManager
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);