🎉 3.4.1.RELEASE 重构swagger封装

This commit is contained in:
smallchill 2022-07-19 12:24:42 +08:00
parent 76f0ea534e
commit 1700db64c5
25 changed files with 153 additions and 64 deletions

View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -101,7 +101,7 @@ mybatis-plus:
swagger:
title: SpringBlade 接口文档系统
description: SpringBlade 接口文档系统
version: 3.4.0
version: 3.4.1
license: Powered By SpringBlade
licenseUrl: https://bladex.vip
terms-of-service-url: https://bladex.vip

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -22,7 +22,7 @@ import $!{pkg};
import lombok.Data;
import lombok.EqualsAndHashCode;
#end
#if($!{swagger2})
#if($!{swagger})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
@ -42,7 +42,7 @@ import io.swagger.annotations.ApiModelProperty;
#if($!{superEntityClass})
@EqualsAndHashCode(callSuper = true)
#end
#if($!{swagger2})
#if($!{swagger})
@ApiModel(value = "$!{entity}对象", description = #if ("$!{table.comment}"=="")"$!{entity}对象"#else"$!{table.comment}"#end)
#end
#if($!{superEntityClass})
@ -66,7 +66,7 @@ public class $!{entity} implements Serializable {
/**
* $!{field.comment}
*/
#if($!{swagger2})
#if($!{swagger})
@ApiModelProperty(value = "$!{field.comment}")
#end
#end

View File

@ -21,7 +21,7 @@ import $!{package.Entity}.$!{entity};
import lombok.Data;
import lombok.EqualsAndHashCode;
#end
#if($!{swagger2})
#if($!{swagger})
import io.swagger.annotations.ApiModel;
#end
@ -35,7 +35,7 @@ import io.swagger.annotations.ApiModel;
@Data
@EqualsAndHashCode(callSuper = true)
#end
#if($!{swagger2})
#if($!{swagger})
@ApiModel(value = "$!{entity}VO对象", description = #if ("$!{table.comment}"=="")"$!{entity}VO对象"#else"$!{table.comment}"#end)
#end
public class $!{entity}VO extends $!{entity} {

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -25,7 +25,7 @@ public interface AppConstant {
/**
* 应用版本
*/
String APPLICATION_VERSION = "3.4.0";
String APPLICATION_VERSION = "3.4.1";
/**
* 基础包

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2028, lengleng (wangiegie@gmail.com).
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import lombok.AllArgsConstructor;
import org.springblade.core.launch.props.BladeProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
@ -44,11 +43,10 @@ import java.util.List;
*
* @author Chill
*/
@AutoConfiguration
@EnableSwagger
@EnableConfigurationProperties(SwaggerProperties.class)
@Import(BeanValidatorPluginsConfiguration.class)
@AutoConfiguration
@AllArgsConstructor
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerAutoConfiguration {
private static final String DEFAULT_BASE_PATH = "/**";
@ -71,13 +69,11 @@ public class SwaggerAutoConfiguration {
}
@Bean
@ConditionalOnMissingBean
public Docket api(SwaggerProperties swaggerProperties) {
// base-path处理
if (swaggerProperties.getBasePath().size() == 0) {
swaggerProperties.getBasePath().add(DEFAULT_BASE_PATH);
}
// exclude-path处理
if (swaggerProperties.getExcludePath().size() == 0) {
swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);
}
@ -85,54 +81,60 @@ public class SwaggerAutoConfiguration {
.host(swaggerProperties.getHost())
.apiInfo(apiInfo(swaggerProperties)).select()
.apis(SwaggerUtil.basePackages(swaggerProperties.getBasePackages()));
swaggerProperties.getBasePath().forEach(p -> apis.paths(PathSelectors.ant(p)));
swaggerProperties.getExcludePath().forEach(p -> apis.paths(PathSelectors.ant(p).negate()));
return apis.build()
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext(swaggerProperties)))
.securityContexts(Lists.newArrayList(securityContext(swaggerProperties)))
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
return apis.build().securityContexts(securityContexts(swaggerProperties)).securitySchemes(securitySchemas(swaggerProperties))
.extensions(openApiExtensionResolver.buildExtensions(bladeProperties.getName()));
}
/**
* 配置默认的全局鉴权策略的开关通过正则表达式进行匹配默认匹配所有URL
*
* @return
*/
private SecurityContext securityContext(SwaggerProperties swaggerProperties) {
return SecurityContext.builder()
private List<SecurityContext> securityContexts(SwaggerProperties swaggerProperties) {
return Collections.singletonList(SecurityContext.builder()
.securityReferences(defaultAuth(swaggerProperties))
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build();
.build());
}
/**
* 默认的全局鉴权策略
*
* @return
*/
private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
return Collections.singletonList(SecurityReference.builder()
.reference(swaggerProperties.getAuthorization().getName())
.scopes(authorizationScopeList.toArray(authorizationScopes))
.build());
List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
List<SecurityReference> securityReferenceList = new ArrayList<>();
List<SwaggerProperties.AuthorizationScope> swaggerScopeList = swaggerProperties.getAuthorization().getAuthorizationScopeList();
swaggerScopeList.forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
if (authorizationScopeList.size() == 0) {
authorizationScopeList.add(new AuthorizationScope("global", "accessEverywhere"));
}
AuthorizationScope[] authorizationScopes = authorizationScopeList.toArray(new AuthorizationScope[0]);
swaggerScopeList.forEach(authorizationScope -> securityReferenceList.add(new SecurityReference(authorizationScope.getName(), authorizationScopes)));
if (securityReferenceList.size() == 0) {
securityReferenceList.add(new SecurityReference(SwaggerUtil.clientInfo().getName(), authorizationScopes));
securityReferenceList.add(new SecurityReference(SwaggerUtil.bladeAuth().getName(), authorizationScopes));
securityReferenceList.add(new SecurityReference(SwaggerUtil.bladeTenant().getName(), authorizationScopes));
}
return securityReferenceList;
}
private OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
ArrayList<GrantType> grantTypes = new ArrayList<>();
swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
/**
* 配置安全策略
*/
private List<SecurityScheme> securitySchemas(SwaggerProperties swaggerProperties) {
List<SwaggerProperties.AuthorizationApiKey> swaggerApiKeyList = swaggerProperties.getAuthorization().getAuthorizationApiKeyList();
if (swaggerApiKeyList.size() == 0) {
return Lists.newArrayList(SwaggerUtil.clientInfo(), SwaggerUtil.bladeAuth(), SwaggerUtil.bladeTenant());
} else {
List<SecurityScheme> securitySchemeList = new ArrayList<>();
swaggerApiKeyList.forEach(authorizationApiKey -> securitySchemeList.add(new ApiKey(authorizationApiKey.getName(), authorizationApiKey.getKeyName(), authorizationApiKey.getPassAs())));
return securitySchemeList;
}
}
/**
* 配置基本信息
*/
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new ApiInfoBuilder()
.title(swaggerProperties.getTitle())

View File

@ -55,7 +55,7 @@ public class SwaggerProperties {
/**
* 版本
**/
private String version = "3.4.0";
private String version = "3.4.1";
/**
* 许可证
**/
@ -120,6 +120,14 @@ public class SwaggerProperties {
*/
private List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
/**
* 鉴权请求头参数列表
*/
private List<AuthorizationApiKey> authorizationApiKeyList = new ArrayList<>();
/**
* 接口匹配地址
*/
private List<String> tokenUrlList = new ArrayList<>();
}
@ -127,6 +135,10 @@ public class SwaggerProperties {
@NoArgsConstructor
public static class AuthorizationScope {
/**
* 鉴权策略名, 需要和ApiKey的name保持一致
*/
private String name = "";
/**
* 作用域名称
*/
@ -138,4 +150,25 @@ public class SwaggerProperties {
private String description = "";
}
@Data
@NoArgsConstructor
public static class AuthorizationApiKey {
/**
* 参数名
*/
private String name = "";
/**
* 参数值
*/
private String keyName = "";
/**
* 参数作用域
*/
private String passAs = "";
}
}

View File

@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2028, lengleng (wangiegie@gmail.com).
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
@ -17,7 +17,9 @@ package org.springblade.core.swagger;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import org.springblade.core.launch.constant.TokenConstant;
import springfox.documentation.RequestHandler;
import springfox.documentation.service.ApiKey;
import java.util.List;
import java.util.function.Predicate;
@ -55,4 +57,17 @@ public class SwaggerUtil {
return Optional.fromNullable(input.declaringClass());
}
public static ApiKey clientInfo() {
return new ApiKey("ClientInfo", "Authorization", "header");
}
public static ApiKey bladeAuth() {
return new ApiKey("BladeAuth", TokenConstant.HEADER, "header");
}
public static ApiKey bladeTenant() {
return new ApiKey("TenantId", "Tenant-Id", "header");
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.core.swagger;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* swagger资源配置
*
* @author Chill
*/
@AutoConfiguration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerWebConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>blade-tool</artifactId>
<groupId>org.springblade</groupId>
<version>3.4.0</version>
<version>3.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<groupId>org.springblade</groupId>
<artifactId>blade-tool</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
<packaging>pom</packaging>
<name>blade-tool</name>
<description>
@ -36,7 +36,7 @@
</scm>
<properties>
<blade.tool.version>3.4.0</blade.tool.version>
<blade.tool.version>3.4.1</blade.tool.version>
<java.version>1.8</java.version>
<maven.plugin.version>3.8.1</maven.plugin.version>
@ -52,7 +52,7 @@
<mica.auto.version>1.2.5</mica.auto.version>
<alibaba.cloud.version>2021.0.1.0</alibaba.cloud.version>
<alibaba.nacos.version>2.1.0</alibaba.nacos.version>
<alibaba.seata.version>1.5.1.1</alibaba.seata.version>
<alibaba.seata.version>1.5.2</alibaba.seata.version>
<spring.plugin.version>2.0.0.RELEASE</spring.plugin.version>
<spring.boot.version>2.7.1</spring.boot.version>