com.iiifi.kite.boot.swagger.SwaggerConfiguration Maven / Gradle / Ivy
package com.iiifi.kite.boot.swagger;
import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import java.util.Collections;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import springfox.documentation.service.*;
import springfox.documentation.schema.ModelRef;
import com.iiifi.kite.configuration.KiteProperties;
import org.springframework.context.annotation.Bean;;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import com.iiifi.kite.boot.properties.KiteSwaggerProperties;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import springfox.documentation.spi.service.contexts.SecurityContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
/**
* Swagger2配置
* @author [email protected] 花朝
* @date 2018/8/18 12:59
*/
@Configuration
@RequiredArgsConstructor
@ConditionalOnClass(Docket.class)
@AutoConfigureAfter(KiteSwaggerProperties.class)
@ConditionalOnProperty(value = "kite.swagger.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingClass("org.springframework.cloud.gateway.configuration.GatewayAutoConfiguration")
public class SwaggerConfiguration {
private final KiteProperties kiteProperties;
private final KiteSwaggerProperties swaggerProperties;
@Bean
public Docket createRestApi() {
// 组名为应用名
String appName = kiteProperties.getName();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.globalOperationParameters(globalHeaders())
.apiInfo(apiInfo(appName)).select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
// 如果开启认证
if (swaggerProperties.getAuthorization().getEnabled()) {
docket.securitySchemes(Collections.singletonList(apiKey()));
docket.securityContexts(Collections.singletonList(securityContext()));
}
return docket;
}
/**
* 配置基于 ApiKey 的鉴权对象
*
* @return {ApiKey}
*/
private ApiKey apiKey() {
return new ApiKey(swaggerProperties.getAuthorization().getName(),
swaggerProperties.getAuthorization().getKeyName(),
ApiKeyVehicle.HEADER.getValue());
}
/**
* 配置默认的全局鉴权策略的开关,以及通过正则表达式进行匹配;默认 ^.*$ 匹配所有URL
* 其中 securityReferences 为配置启用的鉴权策略
*
* @return {SecurityContext}
*/
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build();
}
/**
* 配置默认的全局鉴权策略;其中返回的 SecurityReference 中,reference 即为ApiKey对象里面的name,保持一致才能开启全局鉴权
*
* @return {List}
*/
private List defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(SecurityReference.builder()
.reference(swaggerProperties.getAuthorization().getName())
.scopes(authorizationScopes).build());
}
private ApiInfo apiInfo(String appName) {
String defaultName = appName + " 服务";
String title = Optional.ofNullable(swaggerProperties.getTitle())
.orElse(defaultName);
String description = Optional.ofNullable(swaggerProperties.getDescription())
.orElse(defaultName);
return new ApiInfoBuilder()
.title(title)
.description(description)
.version(swaggerProperties.getVersion())
.contact(new Contact(swaggerProperties.getContactUser(), swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail()))
.build();
}
private List globalHeaders() {
List pars = new ArrayList<>();
getHeaders().forEach(header -> {
Parameter parameter = new ParameterBuilder()
.name(header.getName())
.description(header.getDescription())
.modelRef(new ModelRef("string")).parameterType("header")
.required(header.isRequired())
.build();
pars.add(parameter);
});
return pars;
}
/**
* 设置默认header
* @return headers
*/
private List getHeaders(){
List headers = swaggerProperties.getHeaders();
KiteSwaggerProperties.Header tokenHeader = new KiteSwaggerProperties.Header();
tokenHeader.setName(kiteProperties.getHeaders().getToken());
headers.add(tokenHeader);
KiteSwaggerProperties.Header deviceHeader = new KiteSwaggerProperties.Header();
deviceHeader.setName(kiteProperties.getHeaders().getDeviceId());
headers.add(deviceHeader);
KiteSwaggerProperties.Header keyHeader = new KiteSwaggerProperties.Header();
keyHeader.setName(kiteProperties.getHeaders().getKey());
headers.add(keyHeader);
return headers;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy