pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spi</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-common</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
</dependencies>
WebSwaggerAutoConfiguration.java
package com.gitlab.api.doc.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableOpenApi
@EnableConfigurationProperties(value = {SwaggerConfig.class, ServerProperties.class})
public class WebSwaggerAutoConfiguration {
@Autowired
private SwaggerConfig swaggerConfig;
@Autowired
private ServerProperties serverProperties;
@Value("${web.oauth-client-id}")
private String oauthClientId;
@Value("${web.oauth-client-secret}")
private String oauthClientSecret;
@Value("${web.oauth-authorize-end-point}")
private String oauthAuthorizeEndPoint;
private static final String Security_Scheme_Name = "spring_oauth2";
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(swaggerConfig.getTitle())
.description(swaggerConfig.getDescription())
.version(Version.Number)
.build();
}
@Bean
public Docket commonApiV1() {
return new Docket(DocumentationType.SWAGGER_2)
.host(ObjectUtils.isEmpty(swaggerConfig.getHostname())?"http://localhost:" + serverProperties.getPort():swaggerConfig.getHostname())
.groupName(Version.Number)
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.build()
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(securitySchemes());
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(Collections.singletonList(
new SecurityReference(Security_Scheme_Name, scopes())))
.build();
}
@Bean
public SecurityConfiguration security() {
return SecurityConfigurationBuilder.builder()
.clientId(oauthClientId)
.clientSecret(oauthClientSecret)
.useBasicAuthenticationWithAccessCodeGrant(true)
.build();
}
@Bean
public List<SecurityScheme> securitySchemes() {
List<SecurityScheme> result = new ArrayList<>();
SecurityScheme oauth = new OAuthBuilder().name("oauth2")
.grantTypes(Arrays.asList(new GrantType[]{
new ImplicitGrant(
new LoginEndpoint(oauthAuthorizeEndPoint), "access_token")
}))
.scopes(Arrays.asList(scopes()))
.build();
result.add(oauth);
return result;
}
private AuthorizationScope[] scopes() {
return new AuthorizationScope[]{
new AuthorizationScope("trust", "for read write operations")};
}
private Predicate<String> springBootActuatorJmxPaths() {
return regex("^/(?!env|restart|pause|resume|refresh).*$");
}
public class Version {
public static final String Number = "v1";
public static final String Prefix = "/api/v1/";
public static final String AiVersion = "1.0.0";
}
}
SwaggerConfig.java
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "swagger.config")
@Setter
@Getter
public class SwaggerConfig {
private String hostname;
private String title;
private String description;
private Integer port;
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.gitlab.api.doc.config.WebSwaggerAutoConfiguration
ui使用Knife4j ,单独引入ui界面即可。
子项目引入该jar即可自动生效
最终效果