pub struct RequestContext {
pub user_id: i64,
pub tenant_id: Option<i64>,
pub username: String,
}Expand description
请求上下文(网关透传的用户信息)
网关验证 JWT 后,将用户信息通过 HTTP 头透传到下游服务:
X-User-Id: 用户 IDX-Tenant-Id: 租户 ID(可选)X-Username: 用户名
Fields§
§user_id: i64用户 ID
tenant_id: Option<i64>租户 ID(可选,无租户时为 None)
username: String用户名
Trait Implementations§
Source§impl Clone for RequestContext
impl Clone for RequestContext
Source§fn clone(&self) -> RequestContext
fn clone(&self) -> RequestContext
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RequestContext
impl Debug for RequestContext
Source§impl<S> FromRequestParts<S> for RequestContext
Axum Extractor:从请求扩展中提取 RequestContext(必须认证)
impl<S> FromRequestParts<S> for RequestContext
Axum Extractor:从请求扩展中提取 RequestContext(必须认证)
使用方式:
async fn handler(ctx: RequestContext) -> impl IntoResponse {
format!("Hello, user {}", ctx.user_id)
}如果请求未经网关认证(无 X-User-Id header),返回 401。
Source§impl<S> OptionalFromRequestParts<S> for RequestContext
Axum 0.8 可选提取器:支持 Option<RequestContext> 参数
impl<S> OptionalFromRequestParts<S> for RequestContext
Axum 0.8 可选提取器:支持 Option<RequestContext> 参数
使用方式:
async fn handler(ctx: Option<RequestContext>) -> impl IntoResponse {
match ctx {
Some(c) => format!("Hello, user {}", c.user_id),
None => "Hello, anonymous".to_string(),
}
}当请求未附带用户上下文时返回 Ok(None),不会触发 401 错误。
Auto Trait Implementations§
impl Freeze for RequestContext
impl RefUnwindSafe for RequestContext
impl Send for RequestContext
impl Sync for RequestContext
impl Unpin for RequestContext
impl UnsafeUnpin for RequestContext
impl UnwindSafe for RequestContext
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Perform the extraction.