-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to handle 404 when some routes require authorization #348
Comments
This issue has to due with how
The solution is to re-write it using let public = Router::new()
.route("/public", get(|| async { StatusCode::OK }))
.nest(
"/authorized",
Router::new()
.route("/", get(|| async { StatusCode::OK }))
.layer(RequireAuthorizationLayer::bearer("token")),
);
let not_found = (|| async { StatusCode::NOT_FOUND }).into_service();
let app = public.or(not_found).boxed(); With the current design of |
I have multiple routes that need authorization at the root level and this: let public = Router::new()
.route("/public", get(|| async { StatusCode::OK }))
.nest(
"/",
Router::new()
.route("/first", get(|| async { StatusCode::OK }))
.route("/second", get(|| async { StatusCode::OK }))
.layer(RequireAuthorizationLayer::bearer("token")),
);
let not_found = (|| async { StatusCode::NOT_FOUND }).into_service();
let app = public.or(not_found).boxed(); doesn't seem to work. I guess my only choice is to add the layer to handler individually. Thinking of it, is there any reason for the layer to be applied to the router instead of being recursively applied to existing routes (by |
When you need such fine grained control over which endpoints require auth I would recommend writing an extractor that handles it, and then add that as an argument to your handlers that require auth. That would make things easier to read and you wouldn't have to use
Yes, its how tower works and given axum's ability to route to arbitrary tower services it would be hard to add. |
Bug Report
Version
axum v0.2.5
Description
I have an app that has public and private routes. Private routes are guarded by a
RequireAuthorizationLayer
fromtower-http
like so:When I request a non-existing route the server returns a
401 Unauthorized
response (from theRequireAuthorizationLayer
) instead of a 404. I have tried changing the order of the routes without success.Here is a minimal reproduction repo. Run
cargo test
.The text was updated successfully, but these errors were encountered: