mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-23 05:00:23 +08:00
✨ pig-common-swagger Code optimization, support servlet, webflux, gateway at the same time
This commit is contained in:
parent
dada9fe9dc
commit
477addd483
@ -21,19 +21,19 @@ import com.pig4cloud.pig.common.swagger.config.SwaggerAutoConfiguration;
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 开启 pig swagger
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2020/10/2 开启pig swagger
|
||||
* @date 2020/10/2
|
||||
*/
|
||||
@Target({ ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@EnableSwagger2
|
||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||
@Import({ SwaggerAutoConfiguration.class, GatewaySwaggerAutoConfiguration.class })
|
||||
public @interface EnablePigSwagger2 {
|
||||
|
@ -1,38 +1,35 @@
|
||||
package com.pig4cloud.pig.common.swagger.config;
|
||||
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerResourceHandler;
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerSecurityHandler;
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerUiHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.pig4cloud.pig.common.swagger.support.*;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
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.ComponentScan;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import springfox.documentation.swagger.web.*;
|
||||
|
||||
/**
|
||||
* 网关swagger 配置类,仅在webflux 环境生效哦
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2020/10/2
|
||||
* <p>
|
||||
* 网关swagger 配置类,仅在webflux 环境生效哦
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
|
||||
@ComponentScan("com.pig4cloud.pig.common.swagger.support")
|
||||
public class GatewaySwaggerAutoConfiguration {
|
||||
|
||||
private final SwaggerResourceHandler swaggerResourceHandler;
|
||||
@Bean
|
||||
public SwaggerProvider swaggerProvider(SwaggerProperties swaggerProperties, GatewayProperties gatewayProperties) {
|
||||
return new SwaggerProvider(swaggerProperties, gatewayProperties);
|
||||
}
|
||||
|
||||
private final SwaggerSecurityHandler swaggerSecurityHandler;
|
||||
|
||||
private final SwaggerUiHandler swaggerUiHandler;
|
||||
|
||||
private final SwaggerProperties swaggerProperties;
|
||||
@Bean
|
||||
public SwaggerResourceHandler swaggerResourceHandler(SwaggerProvider swaggerProvider) {
|
||||
return new SwaggerResourceHandler(swaggerProvider);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebFluxSwaggerConfiguration fluxSwaggerConfiguration() {
|
||||
@ -40,7 +37,24 @@ public class GatewaySwaggerAutoConfiguration {
|
||||
}
|
||||
|
||||
@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 匹配路由
|
||||
if (swaggerProperties.getEnabled()) {
|
||||
return RouterFunctions
|
||||
|
@ -17,12 +17,9 @@
|
||||
package com.pig4cloud.pig.common.swagger.config;
|
||||
|
||||
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
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 springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
@ -33,6 +30,7 @@ import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -41,14 +39,21 @@ import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author lengleng swagger配置 禁用方法1:使用注解@Profile({"dev","test"})
|
||||
* swagger配置
|
||||
*
|
||||
* <p>
|
||||
* 禁用方法1:使用注解@Profile({"dev","test"})
|
||||
*
|
||||
* 表示在开发或测试环境开启,而在生产关闭。(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable",
|
||||
*
|
||||
* havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启,生产环境不填则默认关闭Swagger.
|
||||
* </p>
|
||||
*
|
||||
* @author lengleng
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RequiredArgsConstructor
|
||||
@EnableSwagger2
|
||||
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
|
||||
@ConditionalOnMissingClass("org.springframework.cloud.gateway.config.GatewayAutoConfiguration")
|
||||
public class SwaggerAutoConfiguration {
|
||||
|
||||
/**
|
||||
@ -58,10 +63,8 @@ public class SwaggerAutoConfiguration {
|
||||
|
||||
private static final String BASE_PATH = "/**";
|
||||
|
||||
private final SwaggerProperties swaggerProperties;
|
||||
|
||||
@Bean
|
||||
public Docket api() {
|
||||
public Docket api(SwaggerProperties swaggerProperties) {
|
||||
// base-path处理
|
||||
if (swaggerProperties.getBasePath().isEmpty()) {
|
||||
swaggerProperties.getBasePath().add(BASE_PATH);
|
||||
@ -90,23 +93,23 @@ public class SwaggerAutoConfiguration {
|
||||
swaggerProperties.getBasePath().forEach(p -> builder.paths(PathSelectors.ant(p)));
|
||||
swaggerProperties.getExcludePath().forEach(p -> builder.paths(PathSelectors.ant(p).negate()));
|
||||
|
||||
return builder.build().securitySchemes(Collections.singletonList(securitySchema()))
|
||||
.securityContexts(Collections.singletonList(securityContext())).pathMapping("/");
|
||||
return builder.build().securitySchemes(Collections.singletonList(securitySchema(swaggerProperties)))
|
||||
.securityContexts(Collections.singletonList(securityContext(swaggerProperties))).pathMapping("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL
|
||||
* @return
|
||||
*/
|
||||
private SecurityContext securityContext() {
|
||||
return SecurityContext.builder().securityReferences(defaultAuth()).build();
|
||||
private static SecurityContext securityContext(SwaggerProperties swaggerProperties) {
|
||||
return SecurityContext.builder().securityReferences(defaultAuth(swaggerProperties)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认的全局鉴权策略
|
||||
* @return
|
||||
*/
|
||||
private List<SecurityReference> defaultAuth() {
|
||||
private static List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
|
||||
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
||||
swaggerProperties.getAuthorization().getAuthorizationScopeList()
|
||||
.forEach(authorizationScope -> authorizationScopeList.add(
|
||||
@ -117,7 +120,7 @@ public class SwaggerAutoConfiguration {
|
||||
.scopes(authorizationScopeList.toArray(authorizationScopes)).build());
|
||||
}
|
||||
|
||||
private OAuth securitySchema() {
|
||||
private static OAuth securitySchema(SwaggerProperties swaggerProperties) {
|
||||
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
||||
swaggerProperties.getAuthorization().getAuthorizationScopeList()
|
||||
.forEach(authorizationScope -> authorizationScopeList.add(
|
||||
@ -128,7 +131,7 @@ public class SwaggerAutoConfiguration {
|
||||
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())
|
||||
.license(swaggerProperties.getLicense()).licenseUrl(swaggerProperties.getLicenseUrl())
|
||||
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
|
||||
|
@ -19,13 +19,11 @@ package com.pig4cloud.pig.common.swagger.support;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.support.NameUtils;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
|
||||
@ -36,9 +34,7 @@ import java.util.List;
|
||||
* @author Sywd 聚合接口文档注册,和zuul实现类似
|
||||
*/
|
||||
@Primary
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
|
||||
public class SwaggerProvider implements SwaggerResourcesProvider {
|
||||
|
||||
private static final String API_URI = "/v2/api-docs";
|
||||
|
@ -17,11 +17,10 @@
|
||||
|
||||
package com.pig4cloud.pig.common.swagger.support;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
@ -34,8 +33,7 @@ import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
* @date 2018-07-19 SwaggerResourceHandler
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> {
|
||||
|
||||
private final SwaggerResourcesProvider swaggerResources;
|
||||
|
@ -17,31 +17,26 @@
|
||||
|
||||
package com.pig4cloud.pig.common.swagger.support;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import springfox.documentation.swagger.web.SecurityConfiguration;
|
||||
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018-07-19 SwaggerSecurityHandler
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> {
|
||||
|
||||
@Autowired(required = false)
|
||||
private SecurityConfiguration securityConfiguration;
|
||||
private final SecurityConfiguration securityConfiguration;
|
||||
|
||||
/**
|
||||
* Handle the given request.
|
||||
@ -51,8 +46,7 @@ public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> {
|
||||
@Override
|
||||
public Mono<ServerResponse> handle(ServerRequest request) {
|
||||
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(Optional.ofNullable(securityConfiguration)
|
||||
.orElse(SecurityConfigurationBuilder.builder().build())));
|
||||
.body(BodyInserters.fromValue(securityConfiguration));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,31 +17,26 @@
|
||||
|
||||
package com.pig4cloud.pig.common.swagger.support;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import springfox.documentation.swagger.web.UiConfiguration;
|
||||
import springfox.documentation.swagger.web.UiConfigurationBuilder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018-07-19 SwaggerUiHandler
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SwaggerUiHandler implements HandlerFunction<ServerResponse> {
|
||||
|
||||
@Autowired(required = false)
|
||||
private UiConfiguration uiConfiguration;
|
||||
private final UiConfiguration uiConfiguration;
|
||||
|
||||
/**
|
||||
* Handle the given request.
|
||||
@ -50,8 +45,8 @@ public class SwaggerUiHandler implements HandlerFunction<ServerResponse> {
|
||||
*/
|
||||
@Override
|
||||
public Mono<ServerResponse> handle(ServerRequest request) {
|
||||
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters
|
||||
.fromValue(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build())));
|
||||
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(uiConfiguration));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user