|
|
@@ -1,37 +1,36 @@
|
|
|
package cn.hfln.framework.knife4j.doc.configure;
|
|
|
|
|
|
+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 com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
+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;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
-import org.springframework.core.annotation.Order;
|
|
|
-import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
|
|
-import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
-import springfox.documentation.builders.PathSelectors;
|
|
|
-import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
-import springfox.documentation.service.*;
|
|
|
-import springfox.documentation.spi.DocumentationType;
|
|
|
-import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
|
-import springfox.documentation.spring.web.plugins.Docket;
|
|
|
-import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @USER: YangLiu
|
|
|
* knife4j 接口文档配置
|
|
|
*/
|
|
|
@Configuration
|
|
|
-@EnableSwagger2
|
|
|
-@EnableKnife4j
|
|
|
-@Import(BeanValidatorPluginsConfiguration.class)
|
|
|
@EnableConfigurationProperties(DocProperties.class)
|
|
|
@ConditionalOnProperty(value = "lnxx.knife4j.doc.enable", havingValue = "true", matchIfMissing = true)
|
|
|
-public class DocAutoConfigure {
|
|
|
+@Import(SpringDocConfiguration.class)
|
|
|
+public class DocAutoConfigure extends BaseSwaggerConfig {
|
|
|
|
|
|
private final DocProperties properties;
|
|
|
|
|
|
@@ -40,52 +39,60 @@ public class DocAutoConfigure {
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
- @Order(-1)
|
|
|
- public Docket groupRestApi() {
|
|
|
- Docket docket = new Docket(DocumentationType.SWAGGER_2)
|
|
|
- .apiInfo(groupApiInfo())
|
|
|
- .select()
|
|
|
- .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
|
|
|
- .paths(PathSelectors.any())
|
|
|
- .build();
|
|
|
+ @Primary
|
|
|
+ @ConditionalOnMissingBean
|
|
|
+ public OpenAPI customOpenAPI() {
|
|
|
+ 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()) {
|
|
|
- docket.securityContexts(Lists.newArrayList(securityContext()))
|
|
|
- .securitySchemes(Lists.newArrayList(apiKey()));
|
|
|
+ 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 docket;
|
|
|
- }
|
|
|
|
|
|
+ return openAPI;
|
|
|
+ }
|
|
|
|
|
|
- private ApiInfo groupApiInfo() {
|
|
|
- String description = String.format("<div style='font-size:%spx;color:%s;'>%s</div>",
|
|
|
- properties.getDescriptionFontSize(), properties.getDescriptionColor(), properties.getDescription());
|
|
|
- Contact contact = new Contact(properties.getName(), properties.getUrl(), properties.getEmail());
|
|
|
- return new ApiInfoBuilder()
|
|
|
+ @Override
|
|
|
+ public SwaggerProperties swaggerProperties() {
|
|
|
+ return SwaggerProperties.builder()
|
|
|
+ .apiBasePackage(properties.getBasePackage())
|
|
|
.title(properties.getTitle())
|
|
|
- .description(description)
|
|
|
- .termsOfServiceUrl(properties.getTermsOfServiceUrl())
|
|
|
- .contact(contact)
|
|
|
- .license(properties.getLicense())
|
|
|
- .licenseUrl(properties.getLicenseUrl())
|
|
|
+ .description(String.format("<div style='font-size:%spx;color:%s;'>%s</div>",
|
|
|
+ properties.getDescriptionFontSize(), properties.getDescriptionColor(), properties.getDescription()))
|
|
|
.version(properties.getVersion())
|
|
|
+ .contactName(properties.getName())
|
|
|
+ .contactUrl(properties.getUrl())
|
|
|
+ .contactEmail(properties.getEmail())
|
|
|
+ .enableSecurity(properties.isEnableSecurity())
|
|
|
+ .group("default")
|
|
|
+ .serverUrl(properties.getServerUrl())
|
|
|
.build();
|
|
|
}
|
|
|
-
|
|
|
- private ApiKey apiKey() {
|
|
|
- return new ApiKey("Authorization", "Authorization", "header");
|
|
|
- }
|
|
|
-
|
|
|
- private SecurityContext securityContext() {
|
|
|
- return SecurityContext.builder()
|
|
|
- .securityReferences(defaultAuth())
|
|
|
- .forPaths(PathSelectors.regex("/.*"))
|
|
|
- .build();
|
|
|
- }
|
|
|
-
|
|
|
- List<SecurityReference> defaultAuth() {
|
|
|
- AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
- AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
|
- authorizationScopes[0] = authorizationScope;
|
|
|
- return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
|
|
|
- }
|
|
|
}
|