actix / actix/actix-web

Unexpected middleware chaining behaviour - needs explanation / documentation

未關閉
#2,993 0 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Rust
星號
24.8k
分支
1.9k
平均合併
23 小時 10 分鐘
30 天內合併 PR
26

描述

## Expected Behavior
The actix documentation currently mentions:
"Middleware is registered for each App, scope, or Resource and executed in the opposite order as registration"
When I read that phrase I would expect the following to work and call the 'bypass auth middleware' first for the authentication endpoints before the CookieAuthMiddleware
```
wrap(auth_middleware::middleware::CookieAuthMiddlewareFactory::new())
.app_data(app_state.clone())
.service(web::scope("api").configure(controller::api::conf))
.service(
web::scope("oauth2")
.wrap(auth_middleware::middleware::BypassAuthMiddlewareFactory::new())
.configure(auth_middleware::controller::oauth2::conf),
```

## Current Behavior
Currently, it seems that middleware defined within a scope is not called before the 'global' middleware although I consider that later in the 'order of registration'. Maybe the registration in a scope is delayed or something else is amiss. It probably is worthwhile to complete the documentation with more advanced usages of middleware and how multiple middleware would interact with each other in different configurations.

## More context
The equivalent working version of what I intended above is:
```
service(
web::scope("api")
.wrap(auth_middleware::middleware::CookieAuthMiddlewareFactory::new())
.configure(controller::api::conf),
)
.service(
web::scope("oauth2")
.wrap(auth_middleware::middleware::CookieAuthMiddlewareFactory::new())
.wrap(auth_middleware::middleware::BypassAuthMiddlewareFactory::new())
.configure(auth_middleware::controller::oauth2::conf),
```

However, what I really want to achieve here is to have all endpoints protected by default (secure by default) and allow people to override / bypass authentication only in certain cases since I personally have better experiences by securing everything by default and opening up things than having everything open by default and securing each endpoint or group of endpoints separately, it's easy to forget such a line with rather bad effects.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。