From 47cd8d9963945c76355c6f92bac0c07563c1e8b8 Mon Sep 17 00:00:00 2001 From: luozhun <2025254074@qq.com> Date: Wed, 20 Nov 2024 11:45:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(support):=20=E4=BC=98=E5=8C=96=20OpenA?= =?UTF-8?q?piInterceptor=20=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将请求头验证逻辑移至 HandlerMethod 判断之后 -减少了不必要的代码嵌套,提高了代码可读性 --- .../support/interceptor/OpenApiInterceptor.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/policeSecurityServer/src/main/java/com/changhu/support/interceptor/OpenApiInterceptor.java b/policeSecurityServer/src/main/java/com/changhu/support/interceptor/OpenApiInterceptor.java index 99340ff..7ce27fe 100644 --- a/policeSecurityServer/src/main/java/com/changhu/support/interceptor/OpenApiInterceptor.java +++ b/policeSecurityServer/src/main/java/com/changhu/support/interceptor/OpenApiInterceptor.java @@ -23,12 +23,12 @@ public class OpenApiInterceptor implements HandlerInterceptor { @Override public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) { - String header = request.getHeader("X-API-KEY"); - if (StrUtil.isBlank(header)) { - throw new MessageException("请求头缺失"); - } - log.info("apiKey:{} {} 请求:{}", header, LocalDateTime.now(), request.getRequestURI()); if (handler instanceof HandlerMethod handlerMethod) { + String header = request.getHeader("X-API-KEY"); + if (StrUtil.isBlank(header)) { + throw new MessageException("请求头缺失"); + } + log.info("apiKey:{} {} 请求:{}", header, LocalDateTime.now(), request.getRequestURI()); CheckOpenApi methodAnnotation = handlerMethod.getMethodAnnotation(CheckOpenApi.class); if (methodAnnotation != null) { List openApiKeys = methodAnnotation.value().getOpenApiKeys();