feat(auth): 添加 Refresh Token 双 Token 机制

- 添加 /api/refresh-token 接口支持 Token 续期
- 登录接口返回 access_token 和 refresh_token
- 新增 refresh_tokens 表存储 refresh_token
- 部署脚本添加数据库备份和迁移功能
- deploy.sh 添加 4 项 API 测试
- 更新 AGENTS.md 文档
This commit is contained in:
2026-04-19 16:01:41 +08:00
parent 52991dcfd5
commit e14c85436b
11 changed files with 872 additions and 98 deletions

View File

@@ -1,6 +1,5 @@
use actix_web::middleware::from_fn;
use actix_web::{App, HttpServer, web};
use include_dir::{Dir, include_dir};
use tracing::{error, info};
use openssl::ssl::{SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod};
use reqwest::Client;
@@ -21,8 +20,8 @@ use handlers::{
admin_get_user, admin_update_user_payment, add_favorite, create_order,
delete_weather, generate_temp_token_handler, get_current_user_profile,
get_favorites, get_user_quota, get_weather_brief, get_weather_details,
health_check, login, mock_confirm, post_weather_data, remove_favorite,
save_user_profile, serve_static_files,
health_check, login, mock_confirm, post_weather_data, refresh_token,
remove_favorite, root, save_user_profile, serve_static_files,
};
use models::AppState;
@@ -60,10 +59,13 @@ fn create_server_config(
.app_data(web::Data::new(pool))
.app_data(web::Data::new(http_client))
.app_data(web::Data::new(app_state))
// 根路径(无需认证)
.service(root) // #[get("/")] - 返回服务信息
// 静态文件(无需认证)
.service(web::resource("/static/{tail:.*}").route(web::get().to(serve_static_files)))
// API 接口
.service(login) // #[post("/api/login")]
.service(refresh_token) // #[post("/api/refresh-token")](公开接口,无需认证)
.service(get_weather_details) // #[get("/weather/details")](支持 JWT 或 temp_token公开接口
.service(health_check) // #[get("/health")](公开接口,无需认证)
// 受保护接口JWT
@@ -110,6 +112,7 @@ async fn main() -> std::io::Result<()> {
std::env::set_var("SSL_CERT_PATH", &app_config.ssl_cert_path);
std::env::set_var("RUST_LOG", &app_config.rust_log);
std::env::set_var("FREE_USER_DATA_LIMIT", app_config.free_user_data_limit.to_string());
std::env::set_var("APP_VERSION", &app_config.app_version);
}
// 初始化文件日志JSON 格式,带轮转)