token中间件添加注释

This commit is contained in:
李运家 2024-05-30 16:53:20 +08:00
parent b5e96b688a
commit a3875f0110

View File

@ -21,14 +21,16 @@ pub async fn authenticate_access_token(mut req: Request, next: Next) -> Response
let validation = Validation::default(); let validation = Validation::default();
match decode::<Claims>(token, &DecodingKey::from_secret(config!().jwt.token_secret.as_bytes()), &validation) { match decode::<Claims>(token, &DecodingKey::from_secret(config!().jwt.token_secret.as_bytes()), &validation) {
Ok(decoded) => { Ok(decoded) => {
// 从缓存中获取当前用户信息
let account = LOGIN_CACHE.get(&decoded.claims.sub).await; let account = LOGIN_CACHE.get(&decoded.claims.sub).await;
if account.is_none() { if account.is_none() {
return (StatusCode::UNAUTHORIZED, "Invalid token".to_string()).into_response(); return (StatusCode::UNAUTHORIZED, "Invalid token".to_string()).into_response();
} }
let account = account.unwrap(); // 判断token是否有效(注释掉,如果服务因为升级等原因手动重启了,缓存的数据也不再存在)
if account.token != token { // let account = account.unwrap();
return (StatusCode::UNAUTHORIZED, "Invalid token".to_string()).into_response(); // if account.token != token {
} // return (StatusCode::UNAUTHORIZED, "Invalid token".to_string()).into_response();
// }
// 将Claims附加到请求扩展中以便后续处理使用 // 将Claims附加到请求扩展中以便后续处理使用
req.extensions_mut().insert(account); req.extensions_mut().insert(account);
next.run(req).await next.run(req).await