feat: 初始化
This commit is contained in:
+96
@@ -0,0 +1,96 @@
|
||||
package cc.mrbird.febs.common.doc.configure;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||
|
||||
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;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import cc.mrbird.febs.common.doc.properties.FebsDocProperties;
|
||||
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.ApiKey;
|
||||
import springfox.documentation.service.AuthorizationScope;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.service.SecurityReference;
|
||||
import springfox.documentation.service.SecurityScheme;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableKnife4j
|
||||
@Import(BeanValidatorPluginsConfiguration.class)
|
||||
@EnableConfigurationProperties(FebsDocProperties.class)
|
||||
@ConditionalOnProperty(value = "febs.doc.enable", havingValue = "true", matchIfMissing = true)
|
||||
public class FebsDocAutoConfigure {
|
||||
|
||||
private final FebsDocProperties properties;
|
||||
|
||||
public FebsDocAutoConfigure(FebsDocProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(-1)
|
||||
public Docket groupRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(groupApiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
|
||||
.paths(PathSelectors.any())
|
||||
|
||||
.build().securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
|
||||
}
|
||||
|
||||
private ApiInfo groupApiInfo() {
|
||||
String description = String.format("<div style='font-size:%spx;color:%s;'>%s</div>",
|
||||
properties.getDescriptionFontSize(), properties.getDescriptionColor(), properties.getDescription());
|
||||
|
||||
Contact contact = new Contact(properties.getName(), properties.getUrl(), properties.getEmail());
|
||||
|
||||
return new ApiInfoBuilder()
|
||||
.title(properties.getTitle())
|
||||
.description(description)
|
||||
.termsOfServiceUrl(properties.getTermsOfServiceUrl())
|
||||
.contact(contact)
|
||||
.license(properties.getLicense())
|
||||
.licenseUrl(properties.getLicenseUrl())
|
||||
.version(properties.getVersion())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiKey apiKey() {
|
||||
return new ApiKey("BearerToken", "Authorization", "header");
|
||||
}
|
||||
|
||||
private SecurityContext securityContext() {
|
||||
return SecurityContext.builder()
|
||||
.securityReferences(defaultAuth())
|
||||
// 需要鉴权的接口
|
||||
.operationSelector(operationContext -> !operationContext.requestMappingPattern().startsWith("/in/"))
|
||||
.build();
|
||||
}
|
||||
|
||||
List<SecurityReference> defaultAuth() {
|
||||
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
||||
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
||||
authorizationScopes[0] = authorizationScope;
|
||||
return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
|
||||
}
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
package cc.mrbird.febs.common.doc.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "febs.doc")
|
||||
public class FebsDocProperties {
|
||||
|
||||
/**
|
||||
* 是否开启doc功能
|
||||
*/
|
||||
private Boolean enable = true;
|
||||
/**
|
||||
* 接口扫描路径,如Controller路径
|
||||
*/
|
||||
private String basePackage;
|
||||
/**
|
||||
* 文档标题
|
||||
*/
|
||||
private String title;
|
||||
/**
|
||||
* 文档描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 文档描述颜色
|
||||
*/
|
||||
private String descriptionColor = "#42b983";
|
||||
/**
|
||||
* 文档描述字体大小
|
||||
*/
|
||||
private String descriptionFontSize = "14";
|
||||
/**
|
||||
* 服务url
|
||||
*/
|
||||
private String termsOfServiceUrl;
|
||||
/**
|
||||
* 联系方式:姓名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 联系方式:个人网站url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 联系方式:邮箱
|
||||
*/
|
||||
private String email;
|
||||
/**
|
||||
* 协议
|
||||
*/
|
||||
private String license;
|
||||
/**
|
||||
* 协议地址
|
||||
*/
|
||||
private String licenseUrl;
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
private String version;
|
||||
|
||||
public String getBasePackage() {
|
||||
return basePackage;
|
||||
}
|
||||
|
||||
public void setBasePackage(String basePackage) {
|
||||
this.basePackage = basePackage;
|
||||
}
|
||||
|
||||
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 getDescriptionColor() {
|
||||
return descriptionColor;
|
||||
}
|
||||
|
||||
public void setDescriptionColor(String descriptionColor) {
|
||||
this.descriptionColor = descriptionColor;
|
||||
}
|
||||
|
||||
public String getDescriptionFontSize() {
|
||||
return descriptionFontSize;
|
||||
}
|
||||
|
||||
public void setDescriptionFontSize(String descriptionFontSize) {
|
||||
this.descriptionFontSize = descriptionFontSize;
|
||||
}
|
||||
|
||||
public String getTermsOfServiceUrl() {
|
||||
return termsOfServiceUrl;
|
||||
}
|
||||
|
||||
public void setTermsOfServiceUrl(String termsOfServiceUrl) {
|
||||
this.termsOfServiceUrl = termsOfServiceUrl;
|
||||
}
|
||||
|
||||
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 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 getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Boolean getEnable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setEnable(Boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FebsDocProperties{" +
|
||||
"enable=" + enable +
|
||||
", basePackage='" + basePackage + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
", description='" + description + '\'' +
|
||||
", descriptionColor='" + descriptionColor + '\'' +
|
||||
", descriptionFontSize='" + descriptionFontSize + '\'' +
|
||||
", termsOfServiceUrl='" + termsOfServiceUrl + '\'' +
|
||||
", name='" + name + '\'' +
|
||||
", url='" + url + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", license='" + license + '\'' +
|
||||
", licenseUrl='" + licenseUrl + '\'' +
|
||||
", version='" + version + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cc.mrbird.febs.common.doc.configure.FebsDocAutoConfigure
|
||||
Reference in New Issue
Block a user