spring doc:增加模块分组

This commit is contained in:
YunaiV 2023-01-19 15:03:46 +08:00
parent ba65a864fa
commit 711a39b8e9
17 changed files with 264 additions and 70 deletions

View File

@ -6,6 +6,7 @@ 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.media.IntegerSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
@ -27,8 +28,11 @@ import java.util.Optional;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID;
/**
* springdoc 自动配置类
* 使用 knife4j.enable=false 禁用 Swagger
* Swagger 自动配置类基于 OpenAPI + Springdoc 实现
*
* 友情提示
* 1. Springdoc 文档地址<a href="https://github.com/springdoc/springdoc-openapi">仓库</a>
* 2. Swagger 规范 2015 更名为 OpenAPI 规范本质是一个东西
*
* @author 芋道源码
*/
@ -37,53 +41,21 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_
@EnableConfigurationProperties(SwaggerProperties.class)
public class YudaoSwaggerAutoConfiguration {
// ========== 全局 OpenAPI 配置 ==========
@Bean
public OpenAPI createApi(SwaggerProperties properties) {
Map<String, SecurityScheme> maps = securityScheme();
Map<String, SecurityScheme> securitySchemas = buildSecuritySchemes();
OpenAPI openAPI = new OpenAPI()
// 接口信息
.info(buildInfo(properties))
.components(new Components().securitySchemes(maps))
// 接口安全配置
.components(new Components().securitySchemes(securitySchemas))
.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
maps.keySet().forEach(key -> openAPI.addSecurityItem(new SecurityRequirement().addList(key)));
securitySchemas.keySet().forEach(key -> openAPI.addSecurityItem(new SecurityRequirement().addList(key)));
return openAPI;
}
/**
* 自定义 openapi 处理器
*/
@Bean
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
SecurityService securityParser,
SpringDocConfigProperties springDocConfigProperties,
PropertyResolverUtils propertyResolverUtils,
Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers,
Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers,
Optional<JavadocProvider> javadocProvider) {
return new OpenAPIService(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider);
}
@Bean
public GroupedOpenApi appApi() {
return GroupedOpenApi.builder()
.group("app")
.addOperationCustomizer((operation, handlerMethod) ->
operation.addParametersItem(globalHeaderParameter())
)
.pathsToMatch("/app-api/**")
.build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.addOperationCustomizer((operation, handlerMethod) ->
operation.addParametersItem(globalHeaderParameter())
)
.pathsToMatch("/admin-api/**")
.build();
}
/**
* API 摘要信息
*/
@ -99,33 +71,82 @@ public class YudaoSwaggerAutoConfiguration {
/**
* 安全模式这里配置通过请求头 Authorization 传递 token 参数
*/
private Map<String, SecurityScheme> securityScheme() {
Map<String, SecurityScheme> map = new HashMap<>();
private Map<String, SecurityScheme> buildSecuritySchemes() {
Map<String, SecurityScheme> securitySchemes = new HashMap<>();
SecurityScheme securityScheme = new SecurityScheme()
//类型
.type(SecurityScheme.Type.APIKEY)
//请求头的name
.name(HttpHeaders.AUTHORIZATION)
//token所在未知
.in(SecurityScheme.In.HEADER);
map.put(HttpHeaders.AUTHORIZATION, securityScheme);
return map;
.type(SecurityScheme.Type.APIKEY) // 类型
.name(HttpHeaders.AUTHORIZATION) // 请求头的 name
.in(SecurityScheme.In.HEADER); // token 所在位置
securitySchemes.put(HttpHeaders.AUTHORIZATION, securityScheme);
return securitySchemes;
}
/**
* globalHeaderParameter
* 自定义 OpenAPI 处理器
*/
@Bean
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
SecurityService securityParser,
SpringDocConfigProperties springDocConfigProperties,
PropertyResolverUtils propertyResolverUtils,
Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers,
Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers,
Optional<JavadocProvider> javadocProvider) {
return new OpenAPIService(openAPI, securityParser, springDocConfigProperties,
propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider);
}
// ========== 分组 OpenAPI 配置 ==========
/**
* 所有模块的 API 分组
*/
@Bean
public GroupedOpenApi allGroupedOpenApi() {
return buildGroupedOpenApi("all", "");
}
public static GroupedOpenApi buildGroupedOpenApi(String group) {
return buildGroupedOpenApi(group, group);
}
public static GroupedOpenApi buildGroupedOpenApi(String group, String path) {
return GroupedOpenApi.builder()
.group(group)
.pathsToMatch("/admin-api/" + path + "/**", "/app-api/" + path + "/**")
.addOperationCustomizer((operation, handlerMethod) -> operation
.addParametersItem(buildTenantHeaderParameter())
.addParametersItem(buildSecurityHeaderParameter()))
.build();
}
/**
* 构建 Tenant 租户编号请求头参数
*
* @return 多租户参数
*/
private static Parameter globalHeaderParameter() {
private static Parameter buildTenantHeaderParameter() {
return new Parameter()
.name(HEADER_TENANT_ID)
.description("租户编号")
.in(String.valueOf(SecurityScheme.In.HEADER))
.schema(new IntegerSchema()
._default(1L)
.name(HEADER_TENANT_ID)
.description("租户编号")
);
.name(HEADER_TENANT_ID) // header
.description("租户编号") // 描述
.in(String.valueOf(SecurityScheme.In.HEADER)) // 请求 header
.schema(new IntegerSchema()._default(1L).name(HEADER_TENANT_ID).description("租户编号")); // 默认使用租户编号为 1
}
/**
* 构建 Authorization 认证请求头参数
*
* 解决 Knife4j <a href="https://gitee.com/xiaoym/knife4j/issues/I69QBU">Authorize 未生效请求header里未包含参数</a>
*
* @return 认证参数
*/
private static Parameter buildSecurityHeaderParameter() {
return new Parameter()
.name(HttpHeaders.AUTHORIZATION) // header
.description("认证 Token") // 描述
.in(String.valueOf(SecurityScheme.In.HEADER)) // 请求 header
.schema(new StringSchema()._default("Bearer test1").name(HEADER_TENANT_ID).description("认证 Token")); // 默认使用用户编号为 1
}
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.bpm.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* bpm 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class BpmWebConfiguration {
/**
* bpm 模块的 API 分组
*/
@Bean
public GroupedOpenApi bpmGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("bpm");
}
}

View File

@ -0,0 +1,4 @@
/**
* bpm 模块的 web 配置
*/
package cn.iocoder.yudao.module.bpm.framework.web;

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.infra.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* infra 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class InfraWebConfiguration {
/**
* infra 模块的 API 分组
*/
@Bean
public GroupedOpenApi infraGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("infra");
}
}

View File

@ -0,0 +1,4 @@
/**
* infra 模块的 web 配置
*/
package cn.iocoder.yudao.module.infra.framework.web;

View File

@ -0,0 +1,4 @@
mall 后端的代码,暂时归档成一个 yudao-module-mall 的压缩包。
精力有限,近期还是以管理后台的工作流、大屏报表等功能为主!

Binary file not shown.

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.member.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* member 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class MemberWebConfiguration {
/**
* member 模块的 API 分组
*/
@Bean
public GroupedOpenApi memberGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("member");
}
}

View File

@ -0,0 +1,4 @@
/**
* member 模块的 web 配置
*/
package cn.iocoder.yudao.module.member.framework.web;

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.mp.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mp 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class MpWebConfiguration {
/**
* mp 模块的 API 分组
*/
@Bean
public GroupedOpenApi mpGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("mp");
}
}

View File

@ -0,0 +1,4 @@
/**
* mp 模块的 web 配置
*/
package cn.iocoder.yudao.module.mp.framework.web;

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.pay.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* pay 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class PayWebConfiguration {
/**
* pay 模块的 API 分组
*/
@Bean
public GroupedOpenApi payGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("pay");
}
}

View File

@ -0,0 +1,4 @@
/**
* pay 模块的 web 配置
*/
package cn.iocoder.yudao.module.pay.framework.web;

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.system.framework.web.config;
import cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* system 模块的 web 组件的 Configuration
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class SystemWebConfiguration {
/**
* system 模块的 API 分组
*/
@Bean
public GroupedOpenApi systemGroupedOpenApi() {
return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("system");
}
}

View File

@ -0,0 +1,4 @@
/**
* system 模块的 web 配置
*/
package cn.iocoder.yudao.module.system.framework.web;

View File

@ -41,19 +41,19 @@
<artifactId>yudao-spring-boot-starter-biz-error-code</artifactId>
</dependency>
<!-- 数据报表 -->
<!-- <dependency>-->
<!-- <groupId>cn.iocoder.boot</groupId>-->
<!-- <artifactId>yudao-module-visualization-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 工作流 -->
<!-- 数据报表。默认注释,保证编译速度 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-visualization-biz</artifactId>
<version>${revision}</version>
</dependency>
<!-- 工作流。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>cn.iocoder.boot</groupId>-->
<!-- <artifactId>yudao-module-bpm-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 支付服务 -->
<!-- 支付服务。默认注释,保证编译速度 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-pay-biz</artifactId>

View File

@ -34,8 +34,9 @@ spring:
redis:
time-to-live: 1h # 设置过期时间为 1 小时
--- #################### 接口文档配置 ####################
springdoc:
show-actuator: true
swagger-ui:
enabled: true
path: /swagger-ui