pig-common-swagger Code optimization, support servlet, webflux, gateway at the same time

This commit is contained in:
如梦技术 2021-05-19 17:46:52 +08:00
parent dada9fe9dc
commit 477addd483
7 changed files with 67 additions and 67 deletions

View File

@ -21,19 +21,19 @@ import com.pig4cloud.pig.common.swagger.config.SwaggerAutoConfiguration;
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties; import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
* 开启 pig swagger
*
* @author lengleng * @author lengleng
* @date 2020/10/2 开启pig swagger * @date 2020/10/2
*/ */
@Target({ ElementType.TYPE }) @Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Inherited @Inherited
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class) @EnableConfigurationProperties(SwaggerProperties.class)
@Import({ SwaggerAutoConfiguration.class, GatewaySwaggerAutoConfiguration.class }) @Import({ SwaggerAutoConfiguration.class, GatewaySwaggerAutoConfiguration.class })
public @interface EnablePigSwagger2 { public @interface EnablePigSwagger2 {

View File

@ -1,38 +1,35 @@
package com.pig4cloud.pig.common.swagger.config; package com.pig4cloud.pig.common.swagger.config;
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties; import com.pig4cloud.pig.common.swagger.support.*;
import com.pig4cloud.pig.common.swagger.support.SwaggerResourceHandler; import org.springframework.beans.factory.ObjectProvider;
import com.pig4cloud.pig.common.swagger.support.SwaggerSecurityHandler;
import com.pig4cloud.pig.common.swagger.support.SwaggerUiHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import springfox.documentation.swagger.web.*;
/** /**
* 网关swagger 配置类仅在webflux 环境生效哦
*
* @author lengleng * @author lengleng
* @date 2020/10/2 * @date 2020/10/2
* <p>
* 网关swagger 配置类仅在webflux 环境生效哦
*/ */
@RequiredArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ComponentScan("com.pig4cloud.pig.common.swagger.support")
public class GatewaySwaggerAutoConfiguration { public class GatewaySwaggerAutoConfiguration {
private final SwaggerResourceHandler swaggerResourceHandler; @Bean
public SwaggerProvider swaggerProvider(SwaggerProperties swaggerProperties, GatewayProperties gatewayProperties) {
return new SwaggerProvider(swaggerProperties, gatewayProperties);
}
private final SwaggerSecurityHandler swaggerSecurityHandler; @Bean
public SwaggerResourceHandler swaggerResourceHandler(SwaggerProvider swaggerProvider) {
private final SwaggerUiHandler swaggerUiHandler; return new SwaggerResourceHandler(swaggerProvider);
}
private final SwaggerProperties swaggerProperties;
@Bean @Bean
public WebFluxSwaggerConfiguration fluxSwaggerConfiguration() { public WebFluxSwaggerConfiguration fluxSwaggerConfiguration() {
@ -40,7 +37,24 @@ public class GatewaySwaggerAutoConfiguration {
} }
@Bean @Bean
public RouterFunction swaggerRouterFunction() { public SwaggerSecurityHandler swaggerSecurityHandler(
ObjectProvider<SecurityConfiguration> securityConfigurationObjectProvider) {
SecurityConfiguration securityConfiguration = securityConfigurationObjectProvider
.getIfAvailable(() -> SecurityConfigurationBuilder.builder().build());
return new SwaggerSecurityHandler(securityConfiguration);
}
@Bean
public SwaggerUiHandler swaggerUiHandler(ObjectProvider<UiConfiguration> uiConfigurationObjectProvider) {
UiConfiguration uiConfiguration = uiConfigurationObjectProvider
.getIfAvailable(() -> UiConfigurationBuilder.builder().build());
return new SwaggerUiHandler(uiConfiguration);
}
@Bean
public RouterFunction<ServerResponse> swaggerRouterFunction(SwaggerProperties swaggerProperties,
SwaggerUiHandler swaggerUiHandler, SwaggerSecurityHandler swaggerSecurityHandler,
SwaggerResourceHandler swaggerResourceHandler) {
// 开启swagger 匹配路由 // 开启swagger 匹配路由
if (swaggerProperties.getEnabled()) { if (swaggerProperties.getEnabled()) {
return RouterFunctions return RouterFunctions

View File

@ -17,12 +17,9 @@
package com.pig4cloud.pig.common.swagger.config; package com.pig4cloud.pig.common.swagger.config;
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties; import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
@ -33,6 +30,7 @@ import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder; import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -41,14 +39,21 @@ import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
* @author lengleng swagger配置 禁用方法1使用注解@Profile({"dev","test"}) * swagger配置
*
* <p>
* 禁用方法1使用注解@Profile({"dev","test"})
*
* 表示在开发或测试环境开启而在生产关闭推荐使用 禁用方法2使用注解@ConditionalOnProperty(name = "swagger.enable", * 表示在开发或测试环境开启而在生产关闭推荐使用 禁用方法2使用注解@ConditionalOnProperty(name = "swagger.enable",
*
* havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启生产环境不填则默认关闭Swagger. * havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启生产环境不填则默认关闭Swagger.
* </p>
*
* @author lengleng
*/ */
@Configuration @EnableSwagger2
@EnableAutoConfiguration
@RequiredArgsConstructor
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
@ConditionalOnMissingClass("org.springframework.cloud.gateway.config.GatewayAutoConfiguration")
public class SwaggerAutoConfiguration { public class SwaggerAutoConfiguration {
/** /**
@ -58,10 +63,8 @@ public class SwaggerAutoConfiguration {
private static final String BASE_PATH = "/**"; private static final String BASE_PATH = "/**";
private final SwaggerProperties swaggerProperties;
@Bean @Bean
public Docket api() { public Docket api(SwaggerProperties swaggerProperties) {
// base-path处理 // base-path处理
if (swaggerProperties.getBasePath().isEmpty()) { if (swaggerProperties.getBasePath().isEmpty()) {
swaggerProperties.getBasePath().add(BASE_PATH); swaggerProperties.getBasePath().add(BASE_PATH);
@ -90,23 +93,23 @@ public class SwaggerAutoConfiguration {
swaggerProperties.getBasePath().forEach(p -> builder.paths(PathSelectors.ant(p))); swaggerProperties.getBasePath().forEach(p -> builder.paths(PathSelectors.ant(p)));
swaggerProperties.getExcludePath().forEach(p -> builder.paths(PathSelectors.ant(p).negate())); swaggerProperties.getExcludePath().forEach(p -> builder.paths(PathSelectors.ant(p).negate()));
return builder.build().securitySchemes(Collections.singletonList(securitySchema())) return builder.build().securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(securityContext())).pathMapping("/"); .securityContexts(Collections.singletonList(securityContext(swaggerProperties))).pathMapping("/");
} }
/** /**
* 配置默认的全局鉴权策略的开关通过正则表达式进行匹配默认匹配所有URL * 配置默认的全局鉴权策略的开关通过正则表达式进行匹配默认匹配所有URL
* @return * @return
*/ */
private SecurityContext securityContext() { private static SecurityContext securityContext(SwaggerProperties swaggerProperties) {
return SecurityContext.builder().securityReferences(defaultAuth()).build(); return SecurityContext.builder().securityReferences(defaultAuth(swaggerProperties)).build();
} }
/** /**
* 默认的全局鉴权策略 * 默认的全局鉴权策略
* @return * @return
*/ */
private List<SecurityReference> defaultAuth() { private static List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList() swaggerProperties.getAuthorization().getAuthorizationScopeList()
.forEach(authorizationScope -> authorizationScopeList.add( .forEach(authorizationScope -> authorizationScopeList.add(
@ -117,7 +120,7 @@ public class SwaggerAutoConfiguration {
.scopes(authorizationScopeList.toArray(authorizationScopes)).build()); .scopes(authorizationScopeList.toArray(authorizationScopes)).build());
} }
private OAuth securitySchema() { private static OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>(); ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties.getAuthorization().getAuthorizationScopeList() swaggerProperties.getAuthorization().getAuthorizationScopeList()
.forEach(authorizationScope -> authorizationScopeList.add( .forEach(authorizationScope -> authorizationScopeList.add(
@ -128,7 +131,7 @@ public class SwaggerAutoConfiguration {
return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes); return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
} }
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { private static ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()) return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription())
.license(swaggerProperties.getLicense()).licenseUrl(swaggerProperties.getLicenseUrl()) .license(swaggerProperties.getLicense()).licenseUrl(swaggerProperties.getLicenseUrl())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl()) .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())

View File

@ -19,13 +19,11 @@ package com.pig4cloud.pig.common.swagger.support;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider; import springfox.documentation.swagger.web.SwaggerResourcesProvider;
@ -36,9 +34,7 @@ import java.util.List;
* @author Sywd 聚合接口文档注册和zuul实现类似 * @author Sywd 聚合接口文档注册和zuul实现类似
*/ */
@Primary @Primary
@Component
@RequiredArgsConstructor @RequiredArgsConstructor
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerProvider implements SwaggerResourcesProvider { public class SwaggerProvider implements SwaggerResourcesProvider {
private static final String API_URI = "/v2/api-docs"; private static final String API_URI = "/v2/api-docs";

View File

@ -17,11 +17,10 @@
package com.pig4cloud.pig.common.swagger.support; package com.pig4cloud.pig.common.swagger.support;
import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
@ -34,8 +33,7 @@ import springfox.documentation.swagger.web.SwaggerResourcesProvider;
* @date 2018-07-19 SwaggerResourceHandler * @date 2018-07-19 SwaggerResourceHandler
*/ */
@Slf4j @Slf4j
@Component @RequiredArgsConstructor
@AllArgsConstructor
public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> { public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> {
private final SwaggerResourcesProvider swaggerResources; private final SwaggerResourcesProvider swaggerResources;

View File

@ -17,31 +17,26 @@
package com.pig4cloud.pig.common.swagger.support; package com.pig4cloud.pig.common.swagger.support;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration; import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import java.util.Optional;
/** /**
* @author lengleng * @author lengleng
* @date 2018-07-19 SwaggerSecurityHandler * @date 2018-07-19 SwaggerSecurityHandler
*/ */
@Slf4j @Slf4j
@Component @RequiredArgsConstructor
public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> { public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> {
@Autowired(required = false) private final SecurityConfiguration securityConfiguration;
private SecurityConfiguration securityConfiguration;
/** /**
* Handle the given request. * Handle the given request.
@ -51,8 +46,7 @@ public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> {
@Override @Override
public Mono<ServerResponse> handle(ServerRequest request) { public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON) return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(Optional.ofNullable(securityConfiguration) .body(BodyInserters.fromValue(securityConfiguration));
.orElse(SecurityConfigurationBuilder.builder().build())));
} }
} }

View File

@ -17,31 +17,26 @@
package com.pig4cloud.pig.common.swagger.support; package com.pig4cloud.pig.common.swagger.support;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import java.util.Optional;
/** /**
* @author lengleng * @author lengleng
* @date 2018-07-19 SwaggerUiHandler * @date 2018-07-19 SwaggerUiHandler
*/ */
@Slf4j @Slf4j
@Component @RequiredArgsConstructor
public class SwaggerUiHandler implements HandlerFunction<ServerResponse> { public class SwaggerUiHandler implements HandlerFunction<ServerResponse> {
@Autowired(required = false) private final UiConfiguration uiConfiguration;
private UiConfiguration uiConfiguration;
/** /**
* Handle the given request. * Handle the given request.
@ -50,8 +45,8 @@ public class SwaggerUiHandler implements HandlerFunction<ServerResponse> {
*/ */
@Override @Override
public Mono<ServerResponse> handle(ServerRequest request) { public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
.fromValue(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()))); .body(BodyInserters.fromValue(uiConfiguration));
} }
} }