actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

middleware did not take effect

Open Yancey2023 opened this issue 3 months ago • 4 comments

In the following example, the auth middleware did not take effect. It will run create_library directly without identity verification.

web::resource("/library")
        .route(web::get().to(library_handlers::get_libraries))
        .route(web::post().wrap(auth.clone()).to(library_handlers::create_library)),

Your Environment

  • Rust Version: 1.89.0
  • Actix Web Version: 4.11.0

Yancey2023 avatar Sep 04 '25 03:09 Yancey2023

I don't think you can use wrap on route level, it should be either on app or scope/resource level:

        App::new().service(
            web::scope("/library")
                .route("", web::get().to(library_handlers::get_libraries))
                .service(
                    web::resource("")
                        .wrap(auth.clone())
                        .route(web::post().to(library_handlers::create_library)),
                ),
        )

silamon avatar Sep 07 '25 17:09 silamon

You are correct. However, my example can compile successfully, which is indeed misleading.

Yancey2023 avatar Sep 08 '25 01:09 Yancey2023

@Yancey2023 am interested in working on this issue

meshach567 avatar Sep 08 '25 17:09 meshach567

Worth mentioning we do have a relevant test: https://github.com/actix/actix-web/blob/f4ee39cdd707ab4973ec4fe8650e6c78d09f6008/actix-web/src/route.rs#L359-L395

The only difference to OP's code sample is the ordering of .wrap and .to.

robjtede avatar Sep 09 '25 05:09 robjtede