refactor(support): 优化 OpenApiInterceptor 中的代码结构

- 将请求头验证逻辑移至 HandlerMethod 判断之后
-减少了不必要的代码嵌套,提高了代码可读性
This commit is contained in:
luozhun 2024-11-20 11:45:16 +08:00
parent 5f21899c62
commit 47cd8d9963
1 changed files with 5 additions and 5 deletions

View File

@ -23,12 +23,12 @@ public class OpenApiInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) { public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) {
if (handler instanceof HandlerMethod handlerMethod) {
String header = request.getHeader("X-API-KEY"); String header = request.getHeader("X-API-KEY");
if (StrUtil.isBlank(header)) { if (StrUtil.isBlank(header)) {
throw new MessageException("请求头缺失"); throw new MessageException("请求头缺失");
} }
log.info("apiKey:{} {} 请求:{}", header, LocalDateTime.now(), request.getRequestURI()); log.info("apiKey:{} {} 请求:{}", header, LocalDateTime.now(), request.getRequestURI());
if (handler instanceof HandlerMethod handlerMethod) {
CheckOpenApi methodAnnotation = handlerMethod.getMethodAnnotation(CheckOpenApi.class); CheckOpenApi methodAnnotation = handlerMethod.getMethodAnnotation(CheckOpenApi.class);
if (methodAnnotation != null) { if (methodAnnotation != null) {
List<String> openApiKeys = methodAnnotation.value().getOpenApiKeys(); List<String> openApiKeys = methodAnnotation.value().getOpenApiKeys();