Commit ba65a864fa73eda5941c6edb9068794e3b273288
1 parent
fa32611f
perf: 优化Springdoc配置
Showing
1 changed file
with
42 additions
and
23 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/swagger/config/YudaoSwaggerAutoConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.framework.swagger.config; |
| 2 | 2 | |
| 3 | +import io.swagger.v3.oas.models.Components; | |
| 3 | 4 | import io.swagger.v3.oas.models.OpenAPI; |
| 4 | 5 | import io.swagger.v3.oas.models.info.Contact; |
| 5 | 6 | import io.swagger.v3.oas.models.info.Info; |
| ... | ... | @@ -8,7 +9,6 @@ import io.swagger.v3.oas.models.media.IntegerSchema; |
| 8 | 9 | import io.swagger.v3.oas.models.parameters.Parameter; |
| 9 | 10 | import io.swagger.v3.oas.models.security.SecurityRequirement; |
| 10 | 11 | import io.swagger.v3.oas.models.security.SecurityScheme; |
| 11 | -import org.springdoc.core.GroupedOpenApi; | |
| 12 | 12 | import org.springdoc.core.*; |
| 13 | 13 | import org.springdoc.core.customizers.OpenApiBuilderCustomizer; |
| 14 | 14 | import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| ... | ... | @@ -19,7 +19,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties |
| 19 | 19 | import org.springframework.context.annotation.Bean; |
| 20 | 20 | import org.springframework.http.HttpHeaders; |
| 21 | 21 | |
| 22 | +import java.util.HashMap; | |
| 22 | 23 | import java.util.List; |
| 24 | +import java.util.Map; | |
| 23 | 25 | import java.util.Optional; |
| 24 | 26 | |
| 25 | 27 | import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID; |
| ... | ... | @@ -36,29 +38,14 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_ |
| 36 | 38 | public class YudaoSwaggerAutoConfiguration { |
| 37 | 39 | |
| 38 | 40 | @Bean |
| 39 | - public OpenAPI createRestApi(SwaggerProperties properties) { | |
| 40 | - //信息 | |
| 41 | - Info info = new Info() | |
| 42 | - .title(properties.getTitle()) | |
| 43 | - .description(properties.getDescription()) | |
| 44 | - .version(properties.getVersion()) | |
| 45 | - .contact(new Contact().name(properties.getAuthor()).url(properties.getUrl()).email(properties.getEmail())) | |
| 46 | - .license(new License().name(properties.getLicense()).url(properties.getLicenseUrl())); | |
| 47 | - return new OpenAPI() | |
| 48 | - .info(info) | |
| 49 | - .schemaRequirement(HttpHeaders.AUTHORIZATION, securityScheme()) | |
| 41 | + public OpenAPI createApi(SwaggerProperties properties) { | |
| 42 | + Map<String, SecurityScheme> maps = securityScheme(); | |
| 43 | + OpenAPI openAPI = new OpenAPI() | |
| 44 | + .info(buildInfo(properties)) | |
| 45 | + .components(new Components().securitySchemes(maps)) | |
| 50 | 46 | .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)); |
| 51 | - } | |
| 52 | - | |
| 53 | - private SecurityScheme securityScheme() { | |
| 54 | - SecurityScheme securityScheme = new SecurityScheme(); | |
| 55 | - //类型 | |
| 56 | - securityScheme.setType(SecurityScheme.Type.APIKEY); | |
| 57 | - //请求头的name | |
| 58 | - securityScheme.setName(HttpHeaders.AUTHORIZATION); | |
| 59 | - //token所在未知 | |
| 60 | - securityScheme.setIn(SecurityScheme.In.HEADER); | |
| 61 | - return securityScheme; | |
| 47 | + maps.keySet().forEach(key -> openAPI.addSecurityItem(new SecurityRequirement().addList(key))); | |
| 48 | + return openAPI; | |
| 62 | 49 | } |
| 63 | 50 | |
| 64 | 51 | /** |
| ... | ... | @@ -97,6 +84,38 @@ public class YudaoSwaggerAutoConfiguration { |
| 97 | 84 | .build(); |
| 98 | 85 | } |
| 99 | 86 | |
| 87 | + /** | |
| 88 | + * API 摘要信息 | |
| 89 | + */ | |
| 90 | + private Info buildInfo(SwaggerProperties properties) { | |
| 91 | + return new Info() | |
| 92 | + .title(properties.getTitle()) | |
| 93 | + .description(properties.getDescription()) | |
| 94 | + .version(properties.getVersion()) | |
| 95 | + .contact(new Contact().name(properties.getAuthor()).url(properties.getUrl()).email(properties.getEmail())) | |
| 96 | + .license(new License().name(properties.getLicense()).url(properties.getLicenseUrl())); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * 安全模式,这里配置通过请求头 Authorization 传递 token 参数 | |
| 101 | + */ | |
| 102 | + private Map<String, SecurityScheme> securityScheme() { | |
| 103 | + Map<String, SecurityScheme> map = new HashMap<>(); | |
| 104 | + SecurityScheme securityScheme = new SecurityScheme() | |
| 105 | + //类型 | |
| 106 | + .type(SecurityScheme.Type.APIKEY) | |
| 107 | + //请求头的name | |
| 108 | + .name(HttpHeaders.AUTHORIZATION) | |
| 109 | + //token所在未知 | |
| 110 | + .in(SecurityScheme.In.HEADER); | |
| 111 | + map.put(HttpHeaders.AUTHORIZATION, securityScheme); | |
| 112 | + return map; | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * globalHeaderParameter | |
| 117 | + * @return 多租户参数 | |
| 118 | + */ | |
| 100 | 119 | private static Parameter globalHeaderParameter() { |
| 101 | 120 | return new Parameter() |
| 102 | 121 | .name(HEADER_TENANT_ID) | ... | ... |