From 8cc033b86fae92249216ae613c1d36c517c7f3f6 Mon Sep 17 00:00:00 2001 From: JEECG <445654970@qq.com> Date: Wed, 13 Aug 2025 17:22:42 +0800 Subject: [PATCH] =?UTF-8?q?swagger=E4=B8=8A=E9=80=89=E6=8B=A9=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=92=8C=E5=AE=9E=E9=99=85=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=8D=E5=AF=B9=E5=BA=94=20#8705?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jeecg/config/Swagger3Config.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger3Config.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger3Config.java index 0700b463a..0ca420987 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger3Config.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger3Config.java @@ -16,6 +16,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -64,21 +65,59 @@ public class Swagger3Config implements WebMvcConfigurer { @Bean public OperationCustomizer operationCustomizer() { return (operation, handlerMethod) -> { - String path = handlerMethod.getBeanType().getAnnotation(RequestMapping.class).value()[0]; - if (!excludedPaths.contains(path)) { + String path = getFullPath(handlerMethod); + if (!isExcludedPath(path)) { operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)); + }else{ + log.info("忽略加入 X_ACCESS_TOKEN 的 PATH:" + path); } return operation; }; } + + private String getFullPath(HandlerMethod handlerMethod) { + StringBuilder fullPath = new StringBuilder(); + + // 获取类级别的路径 + RequestMapping classMapping = handlerMethod.getBeanType().getAnnotation(RequestMapping.class); + if (classMapping != null && classMapping.value().length > 0) { + fullPath.append(classMapping.value()[0]); + } + + // 获取方法级别的路径 + RequestMapping methodMapping = handlerMethod.getMethodAnnotation(RequestMapping.class); + if (methodMapping != null && methodMapping.value().length > 0) { + String methodPath = methodMapping.value()[0]; + // 确保路径正确拼接,处理斜杠 + if (!fullPath.toString().endsWith("/") && !methodPath.startsWith("/")) { + fullPath.append("/"); + } + fullPath.append(methodPath); + } + + return fullPath.toString(); + } + private boolean isExcludedPath(String path) { + return excludedPaths.stream() + .anyMatch(pattern -> { + if (pattern.endsWith("/**")) { + // 处理通配符匹配 + String basePath = pattern.substring(0, pattern.length() - 3); + return path.startsWith(basePath); + } + // 精确匹配 + return pattern.equals(path); + }); + } + @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info() .title("JeecgBoot 后台服务API接口文档") - .version("3.8.1") + .version("3.8.2") .contact(new Contact().name("北京国炬信息技术有限公司").url("www.jeccg.com").email("jeecgos@163.com")) .description("后台API接口") .termsOfService("NO terms of service")