fix: 写接口限流、WS 有界与 token 不再进日志
新增进程内限流并挂到订单/档案写接口;WS 设 maxPayload、同连接串行处理、按用户限频;鉴权只读请求头并区分登录失效与用户服务故障;访问日志只记路径,避免 token 落盘;陪诊师列表分页夹紧。
This commit is contained in:
+26
-9
@@ -6,19 +6,36 @@ import { HandlerEmployee } from "../handler/employee.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
import { getUserByToken } from "../utils/user_service.js";
|
||||
import { isAdmin } from "../utils/permissions.js";
|
||||
import { rateLimit } from "../middleware/ratelimit.js";
|
||||
|
||||
// 写接口限流:下单最容易造成脏数据与费用纠纷,给最紧的额度;
|
||||
// 修改/取消次之;查询不限制(列表页会轮询刷新)
|
||||
const createLimit = rateLimit(5);
|
||||
const updateLimit = rateLimit(20);
|
||||
const deleteLimit = rateLimit(10);
|
||||
|
||||
/**
|
||||
* 鉴权中间件:校验 token 并将用户信息挂到 ctx.state.user
|
||||
* token→user 结果带 5 分钟进程内缓存(见 utils/user_service.js)
|
||||
*
|
||||
* token 只从请求头读取:曾支持 ?token= 传参,但 access log 会原样记录 URL,
|
||||
* 等于把登录凭证写进日志。另兼容 "Bearer xxx" 头格式,与 user 服务口径一致。
|
||||
*/
|
||||
async function requireAuth(ctx, next) {
|
||||
const token = ctx.header["token"] || ctx.header["authorization"] || ctx.request.query?.token;
|
||||
const raw = ctx.header["token"] || ctx.header["authorization"];
|
||||
const token = raw ? String(raw).replace(/^Bearer\s+/i, "").trim() : "";
|
||||
if (!token) {
|
||||
ResponseUtil.unauthorized(ctx, "缺少token");
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await getUserByToken(token);
|
||||
let user;
|
||||
try {
|
||||
user = await getUserByToken(token, { strict: true });
|
||||
} catch (err) {
|
||||
// 用户服务故障不等于登录失效:区分开才能让前端提示「稍后重试」而不是踢回登录
|
||||
return ResponseUtil.error(ctx, "服务暂不可用,请稍后重试", null, 503);
|
||||
}
|
||||
if (!user) {
|
||||
ResponseUtil.unauthorized(ctx, "登录已失效");
|
||||
return;
|
||||
@@ -63,17 +80,17 @@ function registerRoutes(app) {
|
||||
// 全量查询属管理端能力,必须管理员
|
||||
router.get("/escort-record", requireAuth, requireAdmin, handlerEscortRecord.getRecords.bind(handlerEscortRecord));
|
||||
router.get("/escort-record/:id", requireAuth, handlerEscortRecord.getRecordById.bind(handlerEscortRecord));
|
||||
router.post("/escort-record", requireAuth, handlerEscortRecord.createRecord.bind(handlerEscortRecord));
|
||||
router.put("/escort-record/:id", requireAuth, handlerEscortRecord.updateRecord.bind(handlerEscortRecord));
|
||||
router.patch("/escort-record/:id/status", requireAuth, handlerEscortRecord.updateStatus.bind(handlerEscortRecord));
|
||||
router.delete("/escort-record/:id", requireAuth, handlerEscortRecord.deleteRecord.bind(handlerEscortRecord));
|
||||
router.post("/escort-record", requireAuth, createLimit, handlerEscortRecord.createRecord.bind(handlerEscortRecord));
|
||||
router.put("/escort-record/:id", requireAuth, updateLimit, handlerEscortRecord.updateRecord.bind(handlerEscortRecord));
|
||||
router.patch("/escort-record/:id/status", requireAuth, updateLimit, handlerEscortRecord.updateStatus.bind(handlerEscortRecord));
|
||||
router.delete("/escort-record/:id", requireAuth, deleteLimit, handlerEscortRecord.deleteRecord.bind(handlerEscortRecord));
|
||||
|
||||
// C 端"我的健康档案":userId 一律取自 token,必须注册在 /health-profile/:id 之前
|
||||
router.get("/health-profile/mine", requireAuth, handlerHealthProfile.getMyProfiles.bind(handlerHealthProfile));
|
||||
router.post("/health-profile/mine", requireAuth, handlerHealthProfile.createMyProfile.bind(handlerHealthProfile));
|
||||
router.post("/health-profile/mine", requireAuth, createLimit, handlerHealthProfile.createMyProfile.bind(handlerHealthProfile));
|
||||
router.get("/health-profile/mine/:id", requireAuth, handlerHealthProfile.getMyProfileById.bind(handlerHealthProfile));
|
||||
router.put("/health-profile/mine/:id", requireAuth, handlerHealthProfile.updateMyProfile.bind(handlerHealthProfile));
|
||||
router.delete("/health-profile/mine/:id", requireAuth, handlerHealthProfile.deleteMyProfile.bind(handlerHealthProfile));
|
||||
router.put("/health-profile/mine/:id", requireAuth, updateLimit, handlerHealthProfile.updateMyProfile.bind(handlerHealthProfile));
|
||||
router.delete("/health-profile/mine/:id", requireAuth, deleteLimit, handlerHealthProfile.deleteMyProfile.bind(handlerHealthProfile));
|
||||
|
||||
// 以下为管理端档案接口(C 端一律走 /mine),必须管理员
|
||||
router.get("/health-profile", requireAuth, requireAdmin, handlerHealthProfile.getProfiles.bind(handlerHealthProfile));
|
||||
|
||||
Reference in New Issue
Block a user