程序笔记   发布时间:2022-07-13  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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);
    }
}

大佬总结

以上是大佬教程为你收集整理的Swagger 之 整合 Gateway全部内容,希望文章能够帮你解决Swagger 之 整合 Gateway所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: