1Spring khởi động: SecurityConfig.api tạo new PartnerKeyFilter(keys) và addFilterBefore đặt nó trước BearerTokenAuthenticationFilter; RequestIdFilter có @Order(HIGHEST_PRECEDENCE) nên đứng đầu chuỗi servlet. Cả ba đều kế thừa OncePerRequestFilter, lớp này implement Filter.At startup, SecurityConfig.api creates new PartnerKeyFilter(keys) and addFilterBefore puts it before BearerTokenAuthenticationFilter; RequestIdFilter has @Order(HIGHEST_PRECEDENCE) so it heads the servlet chain. All three extend OncePerRequestFilter, which implements Filter.
2Request GET /api/partner/orders với X-Api-Key=partner-abc vào RequestIdFilter.doFilterInternal: lấy X-Request-Id hoặc tạo UUID, MDC.put("requestId", id) và đặt lại vào response header.GET /api/partner/orders with X-Api-Key=partner-abc enters RequestIdFilter.doFilterInternal: it takes X-Request-Id or generates a UUID, calls MDC.put("requestId", id) and echoes it in the response header.
3RequestIdFilter gọi chain.doFilter(req, res) để chuyền tiếp. PartnerKeyFilter: shouldNotFilter trả false vì URI bắt đầu bằng /api/partner/, validKeys chứa partner-abc nên nó gọi chain.doFilter tiếp.RequestIdFilter calls chain.doFilter(req, res) to pass on. In PartnerKeyFilter, shouldNotFilter returns false because the URI starts with /api/partner/, validKeys contains partner-abc, so it calls chain.doFilter again.
4Không có bearer token nên BearerTokenAuthenticationFilter chuyền tiếp; permitAll cho /api/partner/** cho phép request tới OrderController.listOrders, trả danh sách đơn.With no bearer token, BearerTokenAuthenticationFilter passes on; permitAll for /api/partner/** lets the request reach OrderController.listOrders, which returns the orders.
5Khi phần sau của chuỗi trả về, khối finally trong RequestIdFilter chạy MDC.remove("requestId"), để thread được tái sử dụng không mang mã cũ.When the rest of the chain returns, RequestIdFilter's finally block runs MDC.remove("requestId"), so a reused thread does not carry the old ID.
6Request khác gửi X-Api-Key=wrong-key: RequestIdFilter vẫn gắn mã rồi chuyền tiếp, nhưng PartnerKeyFilter gọi res.sendError(401, "invalid api key") và return mà không gọi chain.doFilter. Filter JWT và OrderController không chạy; finally vẫn dọn MDC.Another request sends X-Api-Key=wrong-key: RequestIdFilter still attaches an ID and passes on, but PartnerKeyFilter calls res.sendError(401, "invalid api key") and returns without chain.doFilter. The JWT filter and OrderController never run; finally still clears MDC.
7Phiên bản Go: main gọi web.Chain(mux, web.Logging, web.PartnerKey(keys)). PartnerKey(keys) trả về một Middleware (closure giữ map key); Chain bọc từ cuối lên bằng h = mws[i](h), nên handler cuối cùng là Logging(PartnerKey(mux)).The Go version: main calls web.Chain(mux, web.Logging, web.PartnerKey(keys)). PartnerKey(keys) returns a Middleware (a closure holding the key map); Chain wraps from the end with h = mws[i](h), so the final handler is Logging(PartnerKey(mux)).
8Request có key partner-abc: Logging ghi start rồi gọi next.ServeHTTP, PartnerKey thấy valid["partner-abc"] = true nên gọi next.ServeHTTP tới mux, listOrders trả JSON. Khi lời gọi quay về, Logging ghi slog.Info kèm thời gian xử lý.A request with key partner-abc: Logging records start and calls next.ServeHTTP, PartnerKey finds valid["partner-abc"] = true and calls next.ServeHTTP on the mux, and listOrders writes the JSON. As the call returns, Logging emits slog.Info with the latency.
9Request Go có key sai: PartnerKey gọi http.Error(w, "invalid api key", 401) rồi return, listOrders không chạy. Vì Logging bọc ngoài cùng, next.ServeHTTP của nó vẫn trả về và request bị chặn vẫn được log.A Go request with a bad key: PartnerKey calls http.Error(w, "invalid api key", 401) and returns, so listOrders never runs. Because Logging wraps everything, its next.ServeHTTP still returns and the rejected request is still logged.