refactor: 更新认证方式为密码验证
简化认证流程,移除用户名,只需要输入访问密码: ## 后端更改 - 移除 JWT_SECRET、ADMIN_USERNAME、ADMIN_PASSWORD 配置 - 添加 ACCESS_PASSWORD 配置 - 更新认证接口:POST /api/auth/verify-password - 简化 JWT payload,只包含认证状态和时间戳 - 移除用户管理相关代码 - 更新认证中间件 ## 前端更改 - 登录页面只显示密码输入框 - 移除用户名输入和相关状态 - 更新 Pinia store 为 verifyPassword 方法 - 移除 User 类型定义 - 更新布局和设置页面 ## 文档更改 - 更新 README.md 说明新的认证方式 - 更新 DEVELOPMENT_GUIDE.md 环境变量配置 - 更新 API.md 认证接口文档 - 更新 .env.example 配置模板 ## 安全改进 - 使用 ACCESS_PASSWORD 作为 JWT 签名密钥 - 自动清理过期会话(7天) - 简化配置,更易于部署 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed626e74dc
commit
903dc1e61f
15 files changed
+124
-171
No files matched your search
@@ -1,14 +1,14 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { config } from '../config';
|
||||
import type { JwtPayload } from '../types';
|
||||
import type { SessionPayload } from '../types';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
// 扩展 Express Request 类型
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: JwtPayload;
|
||||
session?: SessionPayload;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,8 +23,13 @@ export function authMiddleware(req: Request, res: Response, next: NextFunction)
|
||||
const token = authHeader.replace('Bearer ', '');
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
||||
req.user = decoded;
|
||||
const decoded = jwt.verify(token, config.accessPassword) as SessionPayload;
|
||||
|
||||
if (!decoded.authenticated) {
|
||||
return res.status(401).json({ error: '无效的认证令牌' });
|
||||
}
|
||||
|
||||
req.session = decoded;
|
||||
next();
|
||||
} catch (error: any) {
|
||||
logger.error('Token 验证失败', error);
|
||||
@@ -43,8 +48,10 @@ export function optionalAuth(req: Request, res: Response, next: NextFunction) {
|
||||
const token = authHeader.replace('Bearer ', '');
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
||||
req.user = decoded;
|
||||
const decoded = jwt.verify(token, config.accessPassword) as SessionPayload;
|
||||
if (decoded.authenticated) {
|
||||
req.session = decoded;
|
||||
}
|
||||
} catch (error) {
|
||||
// 忽略错误,继续处理请求
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user