优化swagger增强配置

This commit is contained in:
smallchill 2021-01-15 20:37:59 +08:00
parent 17d8fce5f7
commit ef31d33f08

View File

@ -16,7 +16,10 @@
package org.springblade.core.swagger; package org.springblade.core.swagger;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springblade.core.launch.props.BladeProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -45,12 +48,22 @@ import java.util.List;
@EnableSwagger @EnableSwagger
@EnableConfigurationProperties(SwaggerProperties.class) @EnableConfigurationProperties(SwaggerProperties.class)
@Import(BeanValidatorPluginsConfiguration.class) @Import(BeanValidatorPluginsConfiguration.class)
@AllArgsConstructor
public class SwaggerAutoConfiguration { public class SwaggerAutoConfiguration {
private static final String DEFAULT_MAPPING_PATH = "/";
private static final String DEFAULT_BASE_PATH = "/**"; private static final String DEFAULT_BASE_PATH = "/**";
private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**"); private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
/**
* 引入Knife4j扩展类
*/
private final OpenApiExtensionResolver openApiExtensionResolver;
/**
* 引入Blade环境变量
*/
private final BladeProperties bladeProperties;
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() { public SwaggerProperties swaggerProperties() {
@ -77,11 +90,11 @@ public class SwaggerAutoConfiguration {
swaggerProperties.getExcludePath().forEach(p -> apis.paths(PathSelectors.ant(p).negate())); swaggerProperties.getExcludePath().forEach(p -> apis.paths(PathSelectors.ant(p).negate()));
return apis.build() return apis.build()
.securitySchemes(Collections.singletonList(securitySchema())) .securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext())) .securityContexts(Collections.singletonList(securityContext(swaggerProperties)))
.securityContexts(Lists.newArrayList(securityContext())) .securityContexts(Lists.newArrayList(securityContext(swaggerProperties)))
.securitySchemes(Collections.singletonList(securitySchema())) .securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.pathMapping(DEFAULT_MAPPING_PATH); .extensions(openApiExtensionResolver.buildExtensions(bladeProperties.getName()));
} }
/** /**
@ -89,10 +102,10 @@ public class SwaggerAutoConfiguration {
* *
* @return * @return
*/ */
private SecurityContext securityContext() { private SecurityContext securityContext(SwaggerProperties swaggerProperties) {
return SecurityContext.builder() return SecurityContext.builder()
.securityReferences(defaultAuth()) .securityReferences(defaultAuth(swaggerProperties))
.forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex())) .forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build(); .build();
} }
@ -101,23 +114,23 @@ public class SwaggerAutoConfiguration {
* *
* @return * @return
*/ */
private List<SecurityReference> defaultAuth() { private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
return Collections.singletonList(SecurityReference.builder() return Collections.singletonList(SecurityReference.builder()
.reference(swaggerProperties().getAuthorization().getName()) .reference(swaggerProperties.getAuthorization().getName())
.scopes(authorizationScopeList.toArray(authorizationScopes)) .scopes(authorizationScopeList.toArray(authorizationScopes))
.build()); .build());
} }
private OAuth securitySchema() { private OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
ArrayList<GrantType> grantTypes = new ArrayList<>(); ArrayList<GrantType> grantTypes = new ArrayList<>();
swaggerProperties().getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes); return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
} }
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {