initial commit
This commit is contained in:
39
public-common/yelink-doc-starter/pom.xml
Normal file
39
public-common/yelink-doc-starter/pom.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yelink.example</groupId>
|
||||
<artifactId>public-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yelink-doc-starter</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.yelink.doc.configure;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.yelink.doc.properties.DocProperties;
|
||||
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.core.annotation.Order;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.*;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DocAutoconfigure.
|
||||
* @author cwp
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableConfigurationProperties(DocProperties.class)
|
||||
@ConditionalOnProperty(value = "doc.enable", havingValue = "true", matchIfMissing = true)
|
||||
public class DocAutoconfigure {
|
||||
|
||||
private final DocProperties properties;
|
||||
|
||||
public DocAutoconfigure(DocProperties 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())
|
||||
.forPaths(PathSelectors.regex("/.*"))
|
||||
.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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.yelink.doc.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* DocProperties.
|
||||
*
|
||||
* @author cwp
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "doc")
|
||||
@Data
|
||||
public class DocProperties {
|
||||
|
||||
/**
|
||||
* 是否开启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;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.yelink.doc.configure.DocAutoconfigure
|
||||
Reference in New Issue
Block a user