org.zodiac.autoconfigure.apidoc.swagger.AbstractSwaggerConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.apidoc.swagger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.util.CollectionUtils;
import org.zodiac.apidoc.swagger.SwaggerUtil;
import org.zodiac.apidoc.swagger.config.SwaggerDocketInfo;
import org.zodiac.apidoc.swagger.constants.SwaggerSystemPropertiesConstants;
import org.zodiac.autoconfigure.application.ApplicationInfoProperties;
// import springfox.documentation.service.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Import(value = {springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
// @EnableConfigurationProperties(value = {SwaggerProperties.class})
@ConditionalOnClass(value = {springfox.documentation.spi.DocumentationType.class, io.swagger.annotations.Api.class,
com.github.xiaoymin.knife4j.spring.configuration.Knife4jAutoConfiguration.class})
public abstract class AbstractSwaggerConfiguration {
private static final String DEFAULT_BASE_PATH = "/**";
private static final List DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
private final ApplicationInfoProperties applicationInfoProperties;
private final com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver openApiExtensionResolver;
public AbstractSwaggerConfiguration(ApplicationInfoProperties applicationInfoProperties,
com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver openApiExtensionResolver) {
this.applicationInfoProperties = applicationInfoProperties;
this.openApiExtensionResolver = openApiExtensionResolver;
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = SwaggerSystemPropertiesConstants.SWAGGER_PREFIX, ignoreInvalidFields = true)
protected SwaggerProperties swaggerProperties() {
SwaggerProperties swaggerProperties = new SwaggerProperties();
if (swaggerProperties.getDockets().isEmpty())
/*默认的*/
swaggerProperties.getDockets().add(new SwaggerDocketInfo());
return swaggerProperties;
}
protected List apiList(SwaggerProperties swaggerProperties) {
List docketList = null;
if (!CollectionUtils.isEmpty(swaggerProperties.getDockets())) {
docketList = new ArrayList<>();
for (SwaggerDocketInfo docketInfo : swaggerProperties.getDockets()) {
if (docketInfo.getBasePaths().size() == 0) {
docketInfo.getBasePaths().add(DEFAULT_BASE_PATH);
}
if (docketInfo.getExcludePaths().size() == 0) {
docketInfo.getExcludePaths().addAll(DEFAULT_EXCLUDE_PATH);
}
springfox.documentation.spring.web.plugins.ApiSelectorBuilder apis =
new springfox.documentation.spring.web.plugins.Docket(
springfox.documentation.spi.DocumentationType.SWAGGER_2).host(swaggerProperties.getHost())
.apiInfo(apiInfo(swaggerProperties)).select()
.apis(SwaggerUtil.basePackages(docketInfo.getBasePackages()));
docketInfo.getBasePaths().forEach(p -> apis.paths(springfox.documentation.builders.PathSelectors.ant(p)));
docketInfo.getExcludePaths().forEach(p -> apis.paths(springfox.documentation.builders.PathSelectors.ant(p).negate()));
springfox.documentation.spring.web.plugins.Docket docket = apis.build().groupName(docketInfo.getGroup())
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext(swaggerProperties)));
if (null != openApiExtensionResolver) {
docket.extensions(openApiExtensionResolver.buildExtensions(applicationInfoProperties.getName()));
}
docketList.add(docket);
}
}
return docketList;
}
protected springfox.documentation.spring.web.plugins.Docket api(SwaggerProperties swaggerProperties) {
if (swaggerProperties.getBasePaths().size() == 0) {
swaggerProperties.getBasePaths().add(DEFAULT_BASE_PATH);
}
if (swaggerProperties.getExcludePaths().size() == 0) {
swaggerProperties.getExcludePaths().addAll(DEFAULT_EXCLUDE_PATH);
}
springfox.documentation.spring.web.plugins.ApiSelectorBuilder apis =
new springfox.documentation.spring.web.plugins.Docket(
springfox.documentation.spi.DocumentationType.SWAGGER_2).host(swaggerProperties.getHost())
.apiInfo(apiInfo(swaggerProperties)).select()
.apis(SwaggerUtil.basePackages(swaggerProperties.getBasePackages()));
swaggerProperties.getBasePaths().forEach(p -> apis.paths(springfox.documentation.builders.PathSelectors.ant(p)));
swaggerProperties.getExcludePaths().forEach(p -> apis.paths(springfox.documentation.builders.PathSelectors.ant(p).negate()));
return apis.build().groupName(swaggerProperties.getGroup())
.securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext(swaggerProperties)))
.extensions(openApiExtensionResolver.buildExtensions(applicationInfoProperties.getName()));
}
/*
* 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL。
*
*/
private springfox.documentation.spi.service.contexts.SecurityContext
securityContext(SwaggerProperties swaggerProperties) {
return springfox.documentation.spi.service.contexts.SecurityContext.builder()
.securityReferences(defaultAuth(swaggerProperties)).forPaths(springfox.documentation.builders.PathSelectors
.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build();
}
/*
* 默认的全局鉴权策略。
*
*/
private List defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(
authorizationScope -> authorizationScopeList.add(new springfox.documentation.service.AuthorizationScope(
authorizationScope.getScope(), authorizationScope.getDescription())));
springfox.documentation.service.AuthorizationScope[] authorizationScopes =
new springfox.documentation.service.AuthorizationScope[authorizationScopeList.size()];
return Collections.singletonList(springfox.documentation.service.SecurityReference.builder()
.reference(swaggerProperties.getAuthorization().getName())
.scopes(authorizationScopeList.toArray(authorizationScopes)).build());
}
private springfox.documentation.service.OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(
authorizationScope -> authorizationScopeList.add(new springfox.documentation.service.AuthorizationScope(
authorizationScope.getScope(), authorizationScope.getDescription())));
ArrayList grantTypes = new ArrayList<>();
swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes
.add(new springfox.documentation.service.ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
return new springfox.documentation.service.OAuth(swaggerProperties.getAuthorization().getName(),
authorizationScopeList, grantTypes);
}
private springfox.documentation.service.ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new springfox.documentation.builders.ApiInfoBuilder().title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription()).license(swaggerProperties.getLicense())
.licenseUrl(swaggerProperties.getLicenseUrl()).termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.contact(new springfox.documentation.service.Contact(swaggerProperties.getContact().getName(),
swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
.version(swaggerProperties.getVersion()).build();
}
}