From 92923656e368ed0aaa5fa71ceb7dd5a5c8b3f32e Mon Sep 17 00:00:00 2001 From: root Date: Mon, 18 Mar 2024 10:41:46 +0800 Subject: [PATCH] fix: knife4j ui performances --- pom.xml | 9 +- .../properties/PermitAllUrlProperties.java | 3 +- xueyi-common/xueyi-common-swagger/pom.xml | 13 +- .../config/SwaggerAutoConfiguration.java | 111 +++--- .../swagger/config/SwaggerProperties.java | 322 ++++-------------- 5 files changed, 141 insertions(+), 317 deletions(-) diff --git a/pom.xml b/pom.xml index 1942bd94..292c77ea 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,8 @@ 3.2.2 3.5.5 6.1.0 - 2.2.0 + 4.4.0 + 2.3.0 1.27.2 2.3.3 4.3.0 @@ -172,9 +173,9 @@ - org.springdoc - springdoc-openapi-starter-webmvc-ui - ${spring-doc.version} + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter + ${knife-springdoc.version} diff --git a/xueyi-common/xueyi-common-security/src/main/java/com/xueyi/common/security/config/properties/PermitAllUrlProperties.java b/xueyi-common/xueyi-common-security/src/main/java/com/xueyi/common/security/config/properties/PermitAllUrlProperties.java index 4282cce1..5442aebc 100644 --- a/xueyi-common/xueyi-common-security/src/main/java/com/xueyi/common/security/config/properties/PermitAllUrlProperties.java +++ b/xueyi-common/xueyi-common-security/src/main/java/com/xueyi/common/security/config/properties/PermitAllUrlProperties.java @@ -41,7 +41,8 @@ public class PermitAllUrlProperties implements InitializingBean { private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}"); - private static final String[] DEFAULT_IGNORE_URLS = new String[]{"/actuator/**", "/error", "/v3/api-docs"}; + private static final String[] DEFAULT_IGNORE_URLS = new String[]{"/actuator/**", + "/error", "/v3/api-docs", "/v3/api-docs/*", "/doc.html", "/webjars/**"}; /** 常规全部 */ private List routine = new ArrayList<>(); diff --git a/xueyi-common/xueyi-common-swagger/pom.xml b/xueyi-common/xueyi-common-swagger/pom.xml index 6da73d56..d29e8f3f 100644 --- a/xueyi-common/xueyi-common-swagger/pom.xml +++ b/xueyi-common/xueyi-common-swagger/pom.xml @@ -16,18 +16,15 @@ - - - org.springframework.boot - spring-boot-starter-web + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter - - org.springdoc - springdoc-openapi-starter-webmvc-ui + org.projectlombok + lombok + provided - diff --git a/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerAutoConfiguration.java b/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerAutoConfiguration.java index 5727fabb..56f95035 100644 --- a/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerAutoConfiguration.java +++ b/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerAutoConfiguration.java @@ -8,70 +8,69 @@ import io.swagger.v3.oas.models.info.License; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import org.springdoc.core.models.GroupedOpenApi; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.AutoConfiguration; 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; -@Configuration -@EnableAutoConfiguration -@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true) +/** + * Swagger文档配置 + * @author Vondser + * @version 3.5.0 + * @date 2024/02/29 12:12 + */ +@AutoConfiguration +@EnableConfigurationProperties(SwaggerProperties.class) +@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true) public class SwaggerAutoConfiguration { @Bean - @ConditionalOnMissingBean - public SwaggerProperties swaggerProperties() { - return new SwaggerProperties(); - } + public OpenAPI springOpenAPI(SwaggerProperties swaggerProperties) { + return new OpenAPI() + .info(apiInfo(swaggerProperties)) + .components(components()); + } - @Bean - public OpenAPI springOpenAPI() { - return new OpenAPI() - .info(apiInfo(swaggerProperties())) - .components(components()); - } - - private Info apiInfo(SwaggerProperties swaggerProperties) { - return new Info() - .title(swaggerProperties.getTitle()) - .description(swaggerProperties.getDescription()) - .license(license(swaggerProperties)) - .contact(contact(swaggerProperties)) - .version(swaggerProperties.getVersion()); - } + private Info apiInfo(SwaggerProperties swaggerProperties) { + return new Info() + .title(swaggerProperties.getInfo().getTitle()) + .description(swaggerProperties.getInfo().getDescription()) + .license(license(swaggerProperties)) + .contact(contact(swaggerProperties)) + .termsOfService(swaggerProperties.getInfo().getTermsOfService()) + .version(swaggerProperties.getInfo().getVersion()); + } - private License license(SwaggerProperties swaggerProperties) { - return new License() - .name(swaggerProperties.getLicense()) - .url(swaggerProperties.getLicenseUrl()); - } + private License license(SwaggerProperties swaggerProperties) { + return new License() + .name(swaggerProperties.getInfo().getLicense().getName()) + .url(swaggerProperties.getInfo().getLicense().getUrl()); + } - private Contact contact(SwaggerProperties swaggerProperties) { - return new Contact() - .name(swaggerProperties.getContact().getName()) - .url(swaggerProperties.getContact().getUrl()) - .email(swaggerProperties.getContact().getEmail()); - } + private Contact contact(SwaggerProperties swaggerProperties) { + return new Contact() + .name(swaggerProperties.getInfo().getContact().getName()) + .url(swaggerProperties.getInfo().getContact().getName()) + .email(swaggerProperties.getInfo().getContact().getEmail()); + } + /** + * 默认的全局鉴权策略 + */ + private Components components() { + return new Components() + .addSecuritySchemes("token", + new SecurityScheme().type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER).name("Authorization").bearerFormat("JWT")) + ; + } - /** - * 默认的全局鉴权策略 - */ - private Components components() { - return new Components() - .addSecuritySchemes("token", - new SecurityScheme().type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER).name("Authorization").bearerFormat("JWT")) - ; - } - - @Bean - public GroupedOpenApi usersGroup() { - return GroupedOpenApi.builder().group("all") - .addOperationCustomizer((operation, handlerMethod) -> { - operation.addSecurityItem(new SecurityRequirement().addList("token")); - return operation; - }) - .build(); - } -} \ No newline at end of file + @Bean + public GroupedOpenApi usersGroup() { + return GroupedOpenApi.builder().group("all") + .addOperationCustomizer((operation, handlerMethod) -> { + operation.addSecurityItem(new SecurityRequirement().addList("token")); + return operation; + }) + .build(); + } +} diff --git a/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerProperties.java b/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerProperties.java index 23deabe3..df06cd81 100644 --- a/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerProperties.java +++ b/xueyi-common/xueyi-common-swagger/src/main/java/com/xueyi/common/swagger/config/SwaggerProperties.java @@ -1,267 +1,93 @@ package com.xueyi.common.swagger.config; +import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; /** - * swagger 配置读取 + * swagger 配置属性 * - * @author xueyi + * @author Vondser */ -@Component -@ConfigurationProperties("swagger") +@Data +@ConfigurationProperties(prefix = "springdoc") public class SwaggerProperties { - /** 是否开启swagger */ - private Boolean enabled; - - /** swagger会解析的包路径 **/ - private String basePackage = ""; - - /** swagger会解析的url规则 **/ - private List basePath = new ArrayList<>(); - - /** 在basePath基础上需要排除的url规则 **/ - private List excludePath = new ArrayList<>(); - - /** 标题 **/ - private String title = ""; - - /** 描述 **/ - private String description = ""; - - /** 版本 **/ - private String version = ""; - - /** 许可证 **/ - private String license = ""; - - /** 许可证URL **/ - private String licenseUrl = ""; - - /** 服务条款URL **/ - private String termsOfServiceUrl = ""; - - /** host信息 **/ - private String host = ""; - - /** 联系人信息 */ - private Contact contact = new Contact(); - - /** 全局统一鉴权配置 **/ - private Authorization authorization = new Authorization(); - - public Boolean getEnabled() { - return enabled; - } - - public void setEnabled(Boolean enabled) { - this.enabled = enabled; - } - - public String getBasePackage() { - return basePackage; - } - - public void setBasePackage(String basePackage) { - this.basePackage = basePackage; - } - - public List getBasePath() { - return basePath; - } - - public void setBasePath(List basePath) { - this.basePath = basePath; - } - - public List getExcludePath() { - return excludePath; - } - - public void setExcludePath(List excludePath) { - this.excludePath = excludePath; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getLicense() { - return license; - } - - public void setLicense(String license) { - this.license = license; - } - - public String getLicenseUrl() { - return licenseUrl; - } - - public void setLicenseUrl(String licenseUrl) { - this.licenseUrl = licenseUrl; - } - - public String getTermsOfServiceUrl() { - return termsOfServiceUrl; - } - - public void setTermsOfServiceUrl(String termsOfServiceUrl) { - this.termsOfServiceUrl = termsOfServiceUrl; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public Contact getContact() { - return contact; - } - - public void setContact(Contact contact) { - this.contact = contact; - } - - public Authorization getAuthorization() { - return authorization; - } - - public void setAuthorization(Authorization authorization) { - this.authorization = authorization; - } - - public static class Contact { - - /** 联系人 **/ + /** + * 文档基本信息 + */ + private InfoProperties info = new InfoProperties(); + + /** + *

+ * 文档的基础属性信息 + *

+ * + * 为了 springboot 自动生产配置提示信息,所以这里复制一个类出来 + */ + @Data + public static class InfoProperties { + + /** + * 标题 + */ + private String title = null; + + /** + * 描述 + */ + private String description = null; + + /** + * 联系人信息 + */ + private Contact contact = null; + + /** + * 许可证 + */ + private License license = null; + + /** + * 版本 + */ + private String version = null; + + /** + * 服务条款URL + **/ + private String termsOfService = ""; + + } + + @Data + public static class Contact + { + /** + * 联系人 + **/ private String name = ""; - - /** 联系人url **/ + /** + * 联系人url + **/ private String url = ""; - - /** 联系人email **/ + /** + * 联系人email + **/ private String email = ""; - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } } - public static class Authorization { - - /** 鉴权策略ID,需要和SecurityReferences ID保持一致 */ + @Data + public static class License + { + /** + * 联系人 + **/ private String name = ""; + /** + * 联系人url + **/ + private String url = ""; - /** 需要开启鉴权URL的正则 */ - private String authRegex = "^.*$"; - - /** 鉴权作用域列表 */ - private List authorizationScopeList = new ArrayList<>(); - - private List tokenUrlList = new ArrayList<>(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAuthRegex() { - return authRegex; - } - - public void setAuthRegex(String authRegex) { - this.authRegex = authRegex; - } - - public List getAuthorizationScopeList() { - return authorizationScopeList; - } - - public void setAuthorizationScopeList(List authorizationScopeList) { - this.authorizationScopeList = authorizationScopeList; - } - - public List getTokenUrlList() { - return tokenUrlList; - } - - public void setTokenUrlList(List tokenUrlList) { - this.tokenUrlList = tokenUrlList; - } - } - - public static class AuthorizationScope { - - /** 作用域名称 */ - private String scope = ""; - - /** 作用域描述 */ - private String description = ""; - - public String getScope() { - return scope; - } - - public void setScope(String scope) { - this.scope = scope; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } } } \ No newline at end of file -- Gitee