| | |
| | | package com.ruoyi.web.core.config; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import com.ruoyi.common.config.RuoYiConfig; |
| | | import io.swagger.v3.oas.models.Components; |
| | | 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.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; |
| | | import io.swagger.v3.oas.models.security.SecurityScheme.In; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import org.springdoc.core.customizers.OpenApiBuilderCustomizer; |
| | | import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| | | import org.springdoc.core.models.GroupedOpenApi; |
| | | import org.springdoc.core.properties.SpringDocConfigProperties; |
| | | import org.springdoc.core.providers.JavadocProvider; |
| | | import org.springdoc.core.service.OpenAPIService; |
| | | import org.springdoc.core.service.SecurityService; |
| | | import org.springdoc.core.utils.PropertyResolverUtils; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.http.HttpHeaders; |
| | | |
| | | /** |
| | | * Swagger2的接口配置 |
| | | * Swagger 自动配置类,基于 OpenAPI + Springdoc 实现。 |
| | | * |
| | | * @author ruoyi |
| | | * 友情提示: |
| | | * 1. Springdoc 文档地址:<a href="https://github.com/springdoc/springdoc-openapi">仓库</a> |
| | | * 2. Swagger 规范,于 2015 更名为 OpenAPI 规范,本质是一个东西 |
| | | * |
| | | */ |
| | | // @AutoConfiguration |
| | | @Configuration |
| | | @ConditionalOnClass({OpenAPI.class}) |
| | | @EnableConfigurationProperties(SwaggerProperties.class) |
| | | @ConditionalOnProperty(prefix = "springdoc.api-docs", name = "enabled", havingValue = "true", matchIfMissing = true) // 设置为 false 时,禁用 |
| | | public class SwaggerConfig |
| | | { |
| | | /** 系统基础配置 */ |
| | | @Autowired |
| | | private RuoYiConfig ruoyiConfig; |
| | | |
| | | /** |
| | | * 自定义的 OpenAPI 对象 |
| | | */ |
| | | @Bean |
| | | public OpenAPI customOpenApi() |
| | | { |
| | | return new OpenAPI().components(new Components() |
| | | // 设置认证的请求头 |
| | | .addSecuritySchemes("apikey", securityScheme())) |
| | | .addSecurityItem(new SecurityRequirement().addList("apikey")) |
| | | .info(getApiInfo()); |
| | | } |
| | | public static final String HEADER_TENANT_ID = "tenant-id"; |
| | | |
| | | |
| | | // ========== 全局 OpenAPI 配置 ========== |
| | | |
| | | @Bean |
| | | public SecurityScheme securityScheme() |
| | | { |
| | | return new SecurityScheme() |
| | | .type(SecurityScheme.Type.APIKEY) |
| | | .name("Authorization") |
| | | .in(SecurityScheme.In.HEADER) |
| | | .scheme("Bearer"); |
| | | public OpenAPI createApi(SwaggerProperties properties) { |
| | | Map<String, SecurityScheme> securitySchemas = buildSecuritySchemes(); |
| | | OpenAPI openAPI = new OpenAPI() |
| | | // 接口信息 |
| | | .info(buildInfo(properties)) |
| | | // 接口安全配置 |
| | | .components(new Components().securitySchemes(securitySchemas)) |
| | | .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)); |
| | | securitySchemas.keySet().forEach(key -> openAPI.addSecurityItem(new SecurityRequirement().addList(key))); |
| | | return openAPI; |
| | | } |
| | | |
| | | /** |
| | | * 添加摘要信息 |
| | | * API 摘要信息 |
| | | */ |
| | | public Info getApiInfo() |
| | | { |
| | | private Info buildInfo(SwaggerProperties properties) { |
| | | return new Info() |
| | | // 设置标题 |
| | | .title("标题:若依管理系统_接口文档") |
| | | // 描述 |
| | | .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...") |
| | | // 作者信息 |
| | | .contact(new Contact().name(ruoyiConfig.getName())) |
| | | // 版本 |
| | | .version("版本号:" + ruoyiConfig.getVersion()); |
| | | .title(properties.getTitle()) |
| | | .description(properties.getDescription()) |
| | | .version(properties.getVersion()) |
| | | .contact(new Contact().name(properties.getAuthor()).url(properties.getUrl()).email(properties.getEmail())) |
| | | .license(new License().name(properties.getLicense()).url(properties.getLicenseUrl())); |
| | | } |
| | | |
| | | /** |
| | | * 安全模式,这里配置通过请求头 Authorization 传递 token 参数 |
| | | */ |
| | | private Map<String, SecurityScheme> buildSecuritySchemes() { |
| | | Map<String, SecurityScheme> securitySchemes = new HashMap<>(); |
| | | SecurityScheme securityScheme = new SecurityScheme() |
| | | .type(SecurityScheme.Type.APIKEY) // 类型 |
| | | .name(HttpHeaders.AUTHORIZATION) // 请求头的 name |
| | | .in(SecurityScheme.In.HEADER); // token 所在位置 |
| | | securitySchemes.put(HttpHeaders.AUTHORIZATION, securityScheme); |
| | | return securitySchemes; |
| | | } |
| | | |
| | | /** |
| | | * 自定义 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(); |
| | | } |
| | | |
| | | /** |
| | | * 构建 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 |
| | | } |
| | | |
| | | /** |
| | | * 构建 Tenant 租户编号请求头参数 |
| | | * @return 多租户参数 |
| | | */ |
| | | /* private static Parameter buildTenantHeaderParameter() { |
| | | return new Parameter() |
| | | .name(HEADER_TENANT_ID) // header 名 |
| | | .description("租户编号") // 描述 |
| | | .in(String.valueOf(SecurityScheme.In.HEADER)) // 请求 header |
| | | .schema(new IntegerSchema()._default(1L).name(HEADER_TENANT_ID).description("租户编号")); // 默认:使用租户编号为 1 |
| | | } */ |
| | | } |