9个版本
0.3.3 | 2022年12月28日 |
---|---|
0.3.2 | 2022年12月28日 |
0.3.1 | 2022年9月29日 |
0.2.3 | 2022年8月29日 |
0.1.0 | 2022年8月27日 |
798 在 HTTP服务器
每月 32 次下载
30KB
667 代码行
axum_guard_logic
https://crates.io/crates/axum_guard_logic
为 T
实现 Guard
pub trait Guard {
fn check_guard(&self, expected:&Self) -> bool;
}
其中self的值根据FromRequestParts<State> for T
从请求中提取,而expected的值在路由层的层内部给出。
Router::with(state.clone())
.route("/", get(ok))
.layer(
GuardService::new(
state.clone(),
expected.clone()).into_layer()
);
传递给GuardService::new()
的参数state将是FromRequestParts
实现中在T
上调用的状态。
您还可以使用AND/OR逻辑嵌套。
Router::new()
.route("/", get(ok))
.layer(
GuardService::new(state.clone(), expected)
.and(
GuardService::new(
other_state.clone(),
other_expected
)
)
.into_layer()
);
当check_guard
返回false时,将拒绝StatusCode::UNAUTHORIZED
。
依赖关系
~6–8MB
~144K SLoC