Rust 从Actix web应用程序中间件访问应用程序状态
我有一个简单的中间件,用于访问应用程序的全局状态以执行身份验证令牌的验证:Rust 从Actix web应用程序中间件访问应用程序状态,rust,state,middleware,rust-actix,Rust,State,Middleware,Rust Actix,我有一个简单的中间件,用于访问应用程序的全局状态以执行身份验证令牌的验证: use actix_web; use actix_web::HttpMessage; pub struct Authenticator; impl<S> actix_web::middleware::Middleware<S> for Authenticator { fn start( &self, request: &mut actix
use actix_web;
use actix_web::HttpMessage;
pub struct Authenticator;
impl<S> actix_web::middleware::Middleware<S> for Authenticator {
fn start(
&self,
request: &mut actix_web::HttpRequest<S>,
) -> actix_web::Result<actix_web::middleware::Started> {
//let _state = request.state() as &::application::State;
match request.headers().get("Authentication") {
Some(_) => Ok(actix_web::middleware::Started::Done),
None => Err(::view::error(
"No authentication header provided",
actix_web::http::StatusCode::FORBIDDEN,
)),
}
}
}
使用您的状态而不是
S
:
impl actix_web::middleware::Middleware<::Application::State> for Authenticator {
}
impl actix\u web::中间件::验证器中间件{
}
顺便说一下,中间件也可以有状态
impl actix_web::middleware::Middleware<::Application::State> for Authenticator {
}