From c18fb68e81ea7cb7b3841c7304a3202730bfa90d Mon Sep 17 00:00:00 2001 From: EightMonth Date: Tue, 1 Apr 2025 17:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4=EF=BC=8C=E4=B8=BA=E5=85=8D=E7=99=BB=E5=BD=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=8E=92=E9=99=A4token=E6=A0=A1=E9=AA=8C=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=B4?= 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, 37 insertions(+), 8 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 3b9c9a60..ed3880b9 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 @@ -9,6 +9,7 @@ import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; +import lombok.extern.slf4j.Slf4j; import org.jeecg.common.constant.CommonConstant; import org.springdoc.core.GroupedOpenApi; import org.springdoc.core.customizers.GlobalOpenApiCustomizer; @@ -16,11 +17,30 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + /** * @author eightmonth */ +@Slf4j @Configuration public class Swagger3Config implements WebMvcConfigurer { + // 定义不需要注入安全要求的路径集合 + Set excludedPaths = new HashSet<>(Arrays.asList( + "/sys/randomImage/{key}", + "/sys/login", + "/sys/phoneLogin", + "/sys/mLogin", + "/sys/sms", + "/sys/cas/client/validateLogin", + "/test/jeecgDemo/demo3", + "/sys/thirdLogin/**", + "/sys/user/register" + )); + /** * * 显示swagger-ui.html文档展示页,还必须注入swagger资源: @@ -52,12 +72,21 @@ public class Swagger3Config implements WebMvcConfigurer { return openApi -> { // 全局添加鉴权参数 if (openApi.getPaths() != null) { - openApi.getPaths().forEach((s, pathItem) -> { - // 接口添加鉴权参数 - pathItem.readOperations() - .forEach(operation -> - operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)) - ); + openApi.getPaths().forEach((path, pathItem) -> { + log.info("path: {}", path); + // 检查当前路径是否在排除列表中 + boolean isExcluded = excludedPaths.stream().anyMatch(excludedPath -> + excludedPath.equals(path) || + (excludedPath.endsWith("**") && path.startsWith(excludedPath.substring(0, excludedPath.length() - 2))) + ); + + if (!isExcluded) { + // 接口添加鉴权参数 + pathItem.readOperations() + .forEach(operation -> + operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)) + ); + } }); } }; @@ -68,7 +97,7 @@ public class Swagger3Config implements WebMvcConfigurer { return new OpenAPI() .info(new Info() .title("JeecgBoot 后台服务API接口文档") - .version("1.0") + .version("3.7.4") .contact(new Contact().name("北京国炬信息技术有限公司").url("www.jeccg.com").email("jeecgos@163.com")) .description( "后台API接口") .termsOfService("NO terms of service") @@ -77,4 +106,4 @@ public class Swagger3Config implements WebMvcConfigurer { .components(new Components().addSecuritySchemes(CommonConstant.X_ACCESS_TOKEN, new SecurityScheme().name(CommonConstant.X_ACCESS_TOKEN).type(SecurityScheme.Type.HTTP))); } -} +} \ No newline at end of file