From 299f63c6e9fbec5a5d5fba8c19b17613d0113736 Mon Sep 17 00:00:00 2001 From: EightMonth Date: Tue, 1 Apr 2025 15:04:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9token=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=E4=BF=A1=E6=81=AF=E5=8F=8A=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E4=B8=8D=E9=9C=80=E8=A6=81=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jeecg/config/Swagger3Config.java | 25 ++++++++++++++++--- .../src/main/resources/application.yml | 7 +++++- 2 files changed, 27 insertions(+), 5 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 eec425ee..3b9c9a60 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 @@ -11,9 +11,9 @@ import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import org.jeecg.common.constant.CommonConstant; import org.springdoc.core.GroupedOpenApi; +import org.springdoc.core.customizers.GlobalOpenApiCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -43,9 +43,26 @@ public class Swagger3Config implements WebMvcConfigurer { .packagesToExclude("org.jeecg.modules.drag", "org.jeecg.modules.online", "org.jeecg.modules.jmreport") // 加了Operation注解的方法,才生成接口文档 .addOpenApiMethodFilter(method -> method.isAnnotationPresent(Operation.class)) + .addOpenApiCustomiser(globalOpenApiCustomizer()) .build(); } + @Bean + public GlobalOpenApiCustomizer globalOpenApiCustomizer() { + return openApi -> { + // 全局添加鉴权参数 + if (openApi.getPaths() != null) { + openApi.getPaths().forEach((s, pathItem) -> { + // 接口添加鉴权参数 + pathItem.readOperations() + .forEach(operation -> + operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)) + ); + }); + } + }; + } + @Bean public OpenAPI customOpenAPI() { return new OpenAPI() @@ -56,8 +73,8 @@ public class Swagger3Config implements WebMvcConfigurer { .description( "后台API接口") .termsOfService("NO terms of service") .license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0.html"))) - .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)) - .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION, - new SecurityScheme().name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP))); + .addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)) + .components(new Components().addSecuritySchemes(CommonConstant.X_ACCESS_TOKEN, + new SecurityScheme().name(CommonConstant.X_ACCESS_TOKEN).type(SecurityScheme.Type.HTTP))); } } diff --git a/jeecg-boot/jeecg-server-cloud/jeecg-system-cloud-start/src/main/resources/application.yml b/jeecg-boot/jeecg-server-cloud/jeecg-system-cloud-start/src/main/resources/application.yml index 53f5eed6..9841caa3 100644 --- a/jeecg-boot/jeecg-server-cloud/jeecg-system-cloud-start/src/main/resources/application.yml +++ b/jeecg-boot/jeecg-server-cloud/jeecg-system-cloud-start/src/main/resources/application.yml @@ -1,6 +1,11 @@ server: port: 7001 - + +springdoc: + packages-to-exclude: + - org.jeecg.modules.drag + - org.jeecg.modules.online + - org.jeecg.modules.jmreport spring: application: name: jeecg-system