ronghua's picture
up
3c2af29
raw
history blame contribute delete
691 Bytes
use crate::app::{constant::AUTHORIZATION_BEARER_PREFIX, lazy::AUTH_TOKEN};
use axum::{
body::Body,
http::{header::AUTHORIZATION, Request, StatusCode},
middleware::Next,
response::Response,
};
// 认证中间件函数
pub async fn auth_middleware(request: Request<Body>, next: Next) -> Result<Response, StatusCode> {
let auth_header = request
.headers()
.get(AUTHORIZATION)
.and_then(|h| h.to_str().ok())
.and_then(|h| h.strip_prefix(AUTHORIZATION_BEARER_PREFIX))
.ok_or(StatusCode::UNAUTHORIZED)?;
if auth_header != AUTH_TOKEN.as_str() {
return Err(StatusCode::UNAUTHORIZED);
}
Ok(next.run(request).await)
}