token校验增加白名单

This commit is contained in:
李运家 2024-05-30 17:50:33 +08:00
parent a3875f0110
commit fb387614d4
2 changed files with 17 additions and 2 deletions

View File

@ -8,9 +8,10 @@ use tower_http::trace::TraceLayer;
pub(crate) fn init() -> Router {
let open = Router::new().route("/", get(|| async { "hello" }));
let auth = Router::new()
let auth: Router = Router::new()
.route("/account/google", post(controller::account::authenticate_google))
.route("/feedback", post(controller::feedback::add_feedback).get(controller::feedback::get_feedback_list_by_page));
.route("/feedback", post(controller::feedback::add_feedback).get(controller::feedback::get_feedback_list_by_page))
.layer(axum::middleware::from_fn(library::middleware::req_token::authenticate_access_token));
Router::new()
.nest("/", open)

View File

@ -4,8 +4,22 @@ use jsonwebtoken::{decode, DecodingKey, Validation};
use crate::{cache::login_cache::LOGIN_CACHE, config, token::Claims};
const WHITE_LIST: &[(&str, &str)] = &[
("GET", "/api/v1/users/:id"),
("POST", "/api/v1/orders"),
("GET", "/feedback")
];
pub async fn authenticate_access_token(mut req: Request, next: Next) -> Response {
// 获取请求的url和method然后判断是否在白名单中如果在白名单中则直接返回next(req),否则继续执行下面的代码
let method = req.method().clone().to_string();
let uri = req.uri().path_and_query().unwrap().to_string();
if WHITE_LIST.into_iter().find(|item| {
return item.0 == method && item.1 == uri;
}).is_some() {
return next.run(req).await;
}
let auth_header = req.headers().get(header::AUTHORIZATION);
let token = match auth_header {
Some(header_value) => {