how to customize middleware #210
-
customize middlewareMotivationcustomize middleware to receive request and customize response by request header(eg: responde |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
I'm not fully sure what you mean. |
Beta Was this translation helpful? Give feedback.
-
Do you mean something like this https://github.com/tokio-rs/axum/blob/main/examples/global-404-handler/src/main.rs or this #208 ? |
Beta Was this translation helpful? Give feedback.
-
i want customize authorization middleware to applied in axum, if request header |
Beta Was this translation helpful? Give feedback.
-
for example use tower_http::auth::{RequireAuthorizationLayer, AuthorizeRequest};
use hyper::{Request, Response, Body, StatusCode};
use axum::{prelude::*, routing::BoxRoute};
#[derive(Clone)]
struct MyAuth;
impl AuthorizeRequest for MyAuth {
type Output = UserId;
type ResponseBody = Body;
fn authorize<B>(&mut self, request: &Request<B>) -> Option<UserId> {
Some(UserId("123".to_string()))
}
fn on_authorized<B>(&mut self, request: &mut Request<B>, user_id: UserId) {
// Set `user_id` as a request extension so it can be accessed by other
// services down the stack.
request.extensions_mut().insert(user_id);
}
fn unauthorized_response<B>(&mut self, request: &Request<B>) -> Response<Body> {
Response::builder()
.status(StatusCode::OK)
.body(Body::from("request reject because of xxx"))
.unwrap()
}
}
#[derive(Debug)]
struct UserId(String);
pub fn apply_routes() -> BoxRoute<Body>{
route("/", get(|| async {
"hi"
})).layer(RequireAuthorizationLayer::custom(MyAuth)).boxed()
} will get error type mismatch resolving `<MyAuth as AuthorizeRequest>::ResponseBody == http_body::combinators::box_body::BoxBody<hyper::body::Bytes, BoxStdError>`
--> src/lib/authorization.rs:35:56
|
35 | })).layer(RequireAuthorizationLayer::custom(MyAuth)).boxed()
| ^^^^^ expected struct `hyper::Body`, found struct `http_body::combinators::box_body::BoxBody`
|
= note: expected struct `hyper::Body`
found struct `http_body::combinators::box_body::BoxBody<hyper::body::Bytes, BoxStdError>`
= note: required because of the requirements on the impl of `Service<Request<_>>` for `RequireAuthorization<Route<axum::handler::OnMethod<axum::handler::IntoService<[closure@src/lib/authorization.rs:33:18: 35:4], _, ()>, EmptyRouter>, EmptyRouter>, MyAuth>` |
Beta Was this translation helpful? Give feedback.
-
All axum responses must use Change Also, why didn't you say all this in your original question? There was no way for me to know you were talking about |
Beta Was this translation helpful? Give feedback.
All axum responses must use
axum::body::BoxBody
as the response body.MyAuth
is usinghyper::Body
which doesn't work.Change
type ResponseBody = Body;
totype ResponseBody = axum::body::BoxBody;
and create the response with.body(axum::body::box_body(Body::from("request reject because of xxx")))
. Then it'll work.Also, why didn't you say all this in your original question? There was no way for me to know you were talking about
AuthorizeRequest
.