Browse Source

feat: springboot配置项修改 剔除不必要逻辑

yangliu 3 months ago
parent
commit
b1a244ded7

+ 44 - 1
knife4j-doc-spring-boot-starter/src/main/java/cn/hfln/framework/knife4j/doc/configure/DocAutoConfigure.java

@@ -4,6 +4,13 @@ import cn.hfln.framework.knife4j.doc.base.BaseSwaggerConfig;
 import cn.hfln.framework.knife4j.doc.base.SwaggerProperties;
 import cn.hfln.framework.knife4j.doc.properties.DocProperties;
 import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.info.Contact;
+import io.swagger.v3.oas.models.info.Info;
+import io.swagger.v3.oas.models.info.License;
+import io.swagger.v3.oas.models.Components;
+import io.swagger.v3.oas.models.security.SecurityRequirement;
+import io.swagger.v3.oas.models.security.SecurityScheme;
+import io.swagger.v3.oas.models.servers.Server;
 import org.springdoc.core.SpringDocConfiguration;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -13,6 +20,8 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import org.springframework.context.annotation.Primary;
 
+import java.util.stream.Collectors;
+
 /**
  * @USER: YangLiu
  * knife4j 接口文档配置
@@ -33,7 +42,41 @@ public class DocAutoConfigure extends BaseSwaggerConfig {
     @Primary
     @ConditionalOnMissingBean
     public OpenAPI customOpenAPI() {
-        return createOpenAPI(swaggerProperties());
+        OpenAPI openAPI = new OpenAPI()
+                .info(new Info()
+                        .title(properties.getTitle())
+                        .description(String.format("<div style='font-size:%spx;color:%s;'>%s</div>",
+                                properties.getDescriptionFontSize(), properties.getDescriptionColor(), properties.getDescription()))
+                        .version(properties.getVersion())
+                        .contact(new Contact()
+                                .name(properties.getName())
+                                .url(properties.getUrl())
+                                .email(properties.getEmail()))
+                        .license(new License()
+                                .name(properties.getLicense())
+                                .url(properties.getLicenseUrl()))
+                        .termsOfService(properties.getTermsOfServiceUrl()));
+
+        // 只使用 yml 配置的 servers 字段,不设置默认 server
+        if (properties.getServers() != null && !properties.getServers().isEmpty()) {
+            openAPI.setServers(properties.getServers().stream()
+                    .map(cfg -> new Server().url(cfg.getUrl()).description(cfg.getDescription()))
+                    .collect(Collectors.toList()));
+        }
+
+        if (properties.isEnableSecurity()) {
+            openAPI.addSecurityItem(new SecurityRequirement().addList("Bearer"))
+                    .components(new Components()
+                            .addSecuritySchemes("Bearer",
+                                    new SecurityScheme()
+                                            .type(SecurityScheme.Type.HTTP)
+                                            .scheme("bearer")
+                                            .bearerFormat("JWT")
+                                            .in(SecurityScheme.In.HEADER)
+                                            .name("Authorization")));
+        }
+
+        return openAPI;
     }
 
     @Override

+ 20 - 0
src/main/java/cn/hfln/framework/gateway/config/Knife4jDocGroupConfig.java

@@ -0,0 +1,20 @@
+package cn.hfln.framework.gateway.config;
+
+import org.springdoc.core.SwaggerUiConfigParameters;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class Knife4jDocGroupConfig {
+
+    @Bean
+    public CommandLineRunner addGroups(SwaggerUiConfigParameters swaggerUiConfigParameters) {
+        return args -> {
+            // 添加下游服务分组
+            swaggerUiConfigParameters.addGroup("门户服务", "/portal-service-server/v3/api-docs");
+            // 如有更多服务,继续添加
+            // swaggerUiConfigParameters.addGroup("订单服务", "/order-service/v3/api-docs");
+        };
+    }
+} 

+ 40 - 0
src/main/java/cn/hfln/framework/gateway/config/Knife4jGatewayConfig.java

@@ -0,0 +1,40 @@
+package cn.hfln.framework.gateway.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.reactive.function.server.RouterFunction;
+import org.springframework.web.reactive.function.server.RouterFunctions;
+import org.springframework.web.reactive.function.server.ServerResponse;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Configuration
+public class Knife4jGatewayConfig {
+
+    @Bean
+    public RouterFunction<ServerResponse> apiRouter() {
+        return RouterFunctions.route()
+            .GET("/v3/api-docs/swagger-config", request ->
+                ServerResponse.ok().bodyValue(swaggerConfig()))
+            .build();
+    }
+
+    private Map<String, Object> swaggerConfig() {
+        Map<String, Object> config = new HashMap<>();
+        List<Map<String, String>> urls = new ArrayList<>();
+        Map<String, String> portal = new HashMap<>();
+        portal.put("name", "门户服务");
+        portal.put("url", "/portal-service-server/v3/api-docs");
+        urls.add(portal);
+        // 如有更多服务,继续添加
+        // Map<String, String> order = new HashMap<>();
+        // order.put("name", "订单服务");
+        // order.put("url", "/order-service/v3/api-docs");
+        // urls.add(order);
+        config.put("urls", urls);
+        return config;
+    }
+} 

+ 3 - 1
src/main/resources/application.yml

@@ -28,7 +28,9 @@ lnxx:
       description: 网关接口文档
       version: 1.0.0
       base-package: cn.hfln.framework.gateway
-      serverUrl: /portal-service-server
+      servers:
+        - url: /portal-service-server
+          description: 门户服务
 
 springdoc:
   swagger-ui: