Spaces:
Running
Running
File size: 1,491 Bytes
b5e1f18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
const admin = require('../models/admin');
// 验证管理员权限的中间件
function authMiddleware(req, res, next) {
// 跳过登录相关的路由
if (req.path.startsWith('/v1/admin/')) {
return next();
}
// 对静态HTML页面的处理
if (req.path === '/logs.html') {
// 日志页面的访问不在中间件中做验证,而是在前端页面中进行验证
return next();
}
// 修改为:只对管理相关的API进行认证
if (req.path.startsWith('/v1/api-keys') ||
req.path.startsWith('/v1/invalid-cookies') ||
req.path.startsWith('/v1/refresh-cookies') ||
req.path.startsWith('/v1/logs')) {
// 获取Authorization头
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
success: false,
message: '未提供认证token'
});
}
// 提取token
const token = authHeader.split(' ')[1];
// 验证token
const result = admin.verifyToken(token);
if (!result.success) {
return res.status(401).json({
success: false,
message: '无效的token'
});
}
// 将用户信息添加到请求对象
req.admin = {
username: result.username
};
}
next();
}
module.exports = authMiddleware; |