Swagger 之 整合 Gateway
集成到 Gateway ,需要Gateway 上配置属性拦截器,过滤器
通过 Gateway 中配置的路由,将资源全部整合到 swagger,路由集成 参看 Gateway 相关: https://www.cnblogs.com/Alay/p/15150600.html
1、自定义 一个 SwaggerResourcesProvider ;SwaggerResourcesProvider 是一个接口,实现该接口实现自定义逻辑:
public interface SwaggerResourcesProvider extends Supplier<List<SwaggerResource>> { }
如:
@Component @Primary @AllArgsConstructor public class SwaggerProvider implements SwaggerResourcesProvider { private static final String API_URI = "/v2/api-docs"; /** * @See {https://www.cnblogs.com/Alay/p/15150600.html} * 获取 Gateway 中路由信息的 */ private final RouteDefinitionRepository getRouteDefinitions; /** * Swagger 加载资源时忽略的模块 */ private final FilterIgnorePropertiesConfig ignoreServer; @Override public List<SwaggerResource> get() { List<RouteDefinition> routes = new ArrayList<>(); Flux<RouteDefinition> routeDefinitions = getRouteDefinitions.getRouteDefinitions(); routeDefinitions.subscribe(routes::add); List<SwaggerResource> resources = routes.stream() .flatMap(routeDefinition -> routeDefinition.getPredicates().stream() .filter(predicateDefinition -> // 路径与断言配置匹配(忽略大小写) "Path".equalsIgnoreCase(predicateDefinition.getName())) .filter(predicateDefinition -> // swagger忽略加载的模块,Gateway 中 routeId 路由Id !ignoreServer.getSwaggerProviders().contains(routeDefinition.getId())) .map(predicateDefinition -> // 构建Swagger数据源 this.createSwaggerResource( routeDefinition.getId(), // 路由地址处理 predicateDefinition.getArgs() .get(NameUtils.GENERATED_NAME_PREFIX + "0") .replace("/**", API_URI) ) ) ) .sorted(Comparator.comparing(SwaggerResource::getName)) .collect(Collectors.toList()); return resources; } /** * 构建Swagger数据源 * * @param name * @param location * @return */ private SwaggerResource createSwaggerResource(String name, String location) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion("2.0"); return swaggerResource; } }
这里配置的忽略模块配置是从属性文件中配置的,开发中个人根据个人习惯和需求自行处理:
@Data @Configuration @RefreshScope @ConfigurationProperties(prefix = "ignore") public class FilterIgnorePropertiesConfig { private List<String> clients= new ArrayList<>(); private List<String> swaggerProviders = new ArrayList<>(); }
配置文件:
# Swagger忽略的服务 和 网关ID ignore: clients: - test swagger-providers: # 注册中心中服务的名称 - springboot - dfp - vda
编写拦截器:
1、权限认证处理器:
/** * Swagger 权限认证处理器 */ @Component public class SwaggerSecurityHandler implements HandlerFunction<ServerResponse> { @Autowired(required = false) private SecurityConfiguration securityConfiguration; @Override public Mono<ServerResponse> handle(ServerRequest serverRequest) { Mono<ServerResponse> responseMono = ServerResponse.status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters // 新版本为.fromValue() .fromObject(Optional.ofNullable(securityConfiguration) .orElse(SecurityConfigurationBuilder.builder().build()))); return responseMono; } }
2、Swagger UI请求处理器:
@Component public class SwaggerUiHandler implements HandlerFunction<ServerResponse> { @Autowired(required = false) private UiConfiguration uiConfiguration; @Override public Mono<ServerResponse> handle(ServerRequest serverRequest) { Mono<ServerResponse> responseMono = ServerResponse .status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters // 新版本为.fromValue() .fromObject(Optional.ofNullable(uiConfiguration) .orElse(UiConfigurationBuilder.builder().build()))); return responseMono; } }
3:Swagger 集成服务的处理器
@Component @AllArgsConstructor public class SwaggerResourceHandler implements HandlerFunction<ServerResponse> { private final SwaggerResourcesProvider swaggerResources; @Override public Mono<ServerResponse> handle(ServerRequest serverRequest) { Mono<ServerResponse> responseMono = ServerResponse .status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters // 新版本为.fromValue() .fromObject(swaggerResources.get())); return responseMono; } }
将以上配置的配置到路由中:
@Configuration @AllArgsConstructor public class RouterFunctionConfig { /** * 聚合各个服务的swagger接口 */ private final SwaggerResourceHandler swaggerResourceHandler; /** * 权限处理器 */ private final SwaggerSecurityHandler swaggerSecurityHandler; /** * UI处理器 */ private final SwaggerUiHandler swaggerUiHandler; @Bean public RouterFunction routerFunction() { return RouterFunctions .route(RequestPredicates.GET("/swagger-resources") .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler) .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); } }
本文来自博客园,作者:Vermeer,转载请注明原文链接:https://www.cnblogs.com/chxlay/p/15171027.html