pub struct TokenAuthMiddleware<F, V = NoopTokenClaimsValidator> { /* private fields */ }Expand description
Token 鉴权中间件:在 Action 派发前完成 JWT 三重校验并注入当前用户。
挂到 ModuleSpec 后,只对非公开 Action 执行;
标记为 public 的 Action 会绕过本认证中间件,但仍会经过日志、限流、追踪等
通用中间件。对受保护 Action:
- 从
Request::token取Authorization: Bearer <token>; 缺失则短路返回BaseError::Unauthorized。 - 调用
TokenManager::verify_token_checked完成 签名 + 过期 + 黑名单 三重校验;失败短路(TokenVerifyFailed/TokenExpired/TokenRevoked等原样上抛)。 - 用注入的
claims -> User闭包,从已验证的TokenClaims构造User并填入ActionContext.user,随后next.run(ctx)。
用户如何从声明映射(角色/权限放在哪个自定义字段)因项目而异,故由闭包 F 注入。
§示例
ⓘ
use yang_base::action::{TokenAuthMiddleware, User};
use yang_base::definition::{ModuleName, ModuleSpec};
// 从 JWT sub 取用户 ID;业务标识解析失败时必须拒绝认证
let auth = TokenAuthMiddleware::new(|claims| {
let id = claims.sub.parse::<i64>()
.map_err(|_| yang_base::BaseError::Unauthorized("Token subject 无效".into()))?;
Ok(User::new(id, claims.sub.clone()))
});
let module = ModuleSpec::new(ModuleName::new("account.user")?).middleware(auth);Implementations§
Source§impl<F, R> TokenAuthMiddleware<F, NoopTokenClaimsValidator>
impl<F, R> TokenAuthMiddleware<F, NoopTokenClaimsValidator>
Source§impl<F, V> TokenAuthMiddleware<F, V>
impl<F, V> TokenAuthMiddleware<F, V>
Sourcepub fn with_claims_validator<N>(
self,
claims_validator: N,
) -> TokenAuthMiddleware<F, N>where
N: TokenClaimsValidator,
pub fn with_claims_validator<N>(
self,
claims_validator: N,
) -> TokenAuthMiddleware<F, N>where
N: TokenClaimsValidator,
注入应用级异步声明校验器,并保留同一条 Token 认证链。
Sourcepub fn authenticate_public_actions(self) -> Self
pub fn authenticate_public_actions(self) -> Self
在公开 Action 上启用可选认证。
默认情况下,本中间件只处理受保护 Action,以确保登录、刷新等公开端点不会 因缺少 Token 被拦截。启用本选项后,公开 Action 在没有 Authorization header 时仍按匿名请求继续;携带 Bearer Token 时则完成完整校验并注入用户。该模式 适用于请求级 UI 目录等“匿名可用、登录后按身份投影”的公开端点。
非 Bearer Authorization header、无效 Token 和错误 Token 类型不会降级为匿名。
Trait Implementations§
Source§impl<F, R, V> Middleware for TokenAuthMiddleware<F, V>where
F: Fn(&TokenClaims) -> R + Send + Sync + 'static,
R: IntoUserProjection + Send + Sync + 'static,
V: TokenClaimsValidator,
impl<F, R, V> Middleware for TokenAuthMiddleware<F, V>where
F: Fn(&TokenClaims) -> R + Send + Sync + 'static,
R: IntoUserProjection + Send + Sync + 'static,
V: TokenClaimsValidator,
Source§fn role(&self) -> MiddlewareRole
fn role(&self) -> MiddlewareRole
返回构建期可验证的安全依赖角色。
Source§fn scope(&self) -> MiddlewareScope
fn scope(&self) -> MiddlewareScope
返回此中间件适用的 Action 范围。 Read more
Auto Trait Implementations§
impl<F, V> Freeze for TokenAuthMiddleware<F, V>
impl<F, V> RefUnwindSafe for TokenAuthMiddleware<F, V>where
F: RefUnwindSafe,
V: RefUnwindSafe,
impl<F, V> Send for TokenAuthMiddleware<F, V>
impl<F, V> Sync for TokenAuthMiddleware<F, V>
impl<F, V> Unpin for TokenAuthMiddleware<F, V>
impl<F, V> UnsafeUnpin for TokenAuthMiddleware<F, V>where
F: UnsafeUnpin,
V: UnsafeUnpin,
impl<F, V> UnwindSafe for TokenAuthMiddleware<F, V>where
F: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
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> 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> ⓘ
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 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> ⓘ
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