Get Extracted Data in Middleware #3461
Answered
by
amitnos123
amitnos123
asked this question in
Q&A
-
SummaryMy route pub fn get_router() -> Router<AppState> {
let router: Router<AppState> = Router::new()
.route(....)
.route_layer(middleware::from_fn(process_claims))
.route_layer(middleware::from_extractor::<Claims>());
router
} I'm trying to extract Claims in the inner middleware, after the extractor. pub async fn process_claims(
req: Request,
next: Next,
) -> Result<Response, StatusCode> {
tracing::info!("process_claims");
tracing::info!("is none: {}", &req.extensions().get::<Claims>().is_none());
if let Some(claims) = req.extensions().get::<Claims>() {
if claims.is_auth() {
return Ok(next.run(req).await);
}
}
tracing::error!("Token Middleware - Missing Claims");
Err(StatusCode::INTERNAL_SERVER_ERROR)
} The log always returns the error message. axum version0.8.4 |
Beta Was this translation helpful? Give feedback.
Answered by
amitnos123
Sep 10, 2025
Replies: 1 comment
-
Solved // Request → Claims extractor → process_claims → handler
pub fn get_router() -> Router<AppState> {
let router: Router<AppState> = Router::new()
.route(....)
.route_layer(middleware::from_fn(process_claims))
.route_layer(middleware::from_extractor::<Claims>());
router
}
/// Middleware
pub async fn process_claims(
c: Claims,
mut req: Request,
next: Next,
) -> Result<Response, StatusCode> {
if c.is_auth() {
req.extensions_mut().insert(c.clone());
return Ok(next.run(req).await);
}
tracing::error!("Token Middleware - Missing Claims");
Err(StatusCode::INTERNAL_SERVER_ERROR)
} Tower_http layers work in first in last out
After extracting, does the processing. For impl<S> FromRequestParts<S> for Claims
where
S: Send + Sync, Feel free to ask questions about what I did, if someone need help |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
amitnos123
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved
Tower_http layers work in first in last out
.ro…