19. 开发人员指南

这些是编写网关的一些自定义组件的基本指南。spring-doc.cn

19.1. 编写自定义路由谓词工厂

为了编写 Route Predicate,您需要作为 bean 实现。有一个名为 to can extend 的抽象类。RoutePredicateFactoryAbstractRoutePredicateFactoryspring-doc.cn

MyRoutePredicateFactory.java
@Component
public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<MyRoutePredicateFactory.Config> {

    public MyRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        // grab configuration from Config object
        return exchange -> {
            //grab the request
            ServerHttpRequest request = exchange.getRequest();
            //take information from the request to see if it
            //matches configuration.
            return matches(config, request);
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}

19.2. 编写自定义 GatewayFilter 工厂

要编写 ,必须作为 Bean 实现。 您可以扩展一个名为 . 以下示例说明如何执行此操作:GatewayFilterGatewayFilterFactoryAbstractGatewayFilterFactoryspring-doc.cn

例 75.PreGatewayFilterFactory.java
@Component
public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory<PreGatewayFilterFactory.Config> {

    public PreGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            //If you want to build a "pre" filter you need to manipulate the
            //request before calling chain.filter
            ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
            //use builder to manipulate the request
            return chain.filter(exchange.mutate().request(builder.build()).build());
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}
PostGatewayFilterFactory.java
@Component
public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory<PostGatewayFilterFactory.Config> {

    public PostGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                ServerHttpResponse response = exchange.getResponse();
                //Manipulate the response in some way
            }));
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }

}

19.2.1. 在配置中命名自定义过滤器和引用

自定义过滤器类名称应以 .GatewayFilterFactoryspring-doc.cn

例如,要引用配置文件中命名的过滤器,过滤器 必须位于名为 的类中。SomethingSomethingGatewayFilterFactoryspring-doc.cn

可以创建一个不带后缀的网关筛选条件,例如 .此筛选器可以是 引用 as in configuration files.这不是受支持的命名 约定,并且此语法可能会在将来的发行版中删除。请更新过滤器 name 来合规。GatewayFilterFactoryclass AnotherThingAnotherThing

19.3. 编写自定义全局过滤器

要编写自定义全局过滤器,必须将 interface 实现为 Bean。 这会将筛选条件应用于所有请求。GlobalFilterspring-doc.cn

以下示例分别说明如何设置全局 pre 和 post 过滤器:spring-doc.cn

@Bean
public GlobalFilter customGlobalFilter() {
    return (exchange, chain) -> exchange.getPrincipal()
        .map(Principal::getName)
        .defaultIfEmpty("Default User")
        .map(userName -> {
          //adds header to proxied request
          exchange.getRequest().mutate().header("CUSTOM-REQUEST-HEADER", userName).build();
          return exchange;
        })
        .flatMap(chain::filter);
}

@Bean
public GlobalFilter customGlobalPostFilter() {
    return (exchange, chain) -> chain.filter(exchange)
        .then(Mono.just(exchange))
        .map(serverWebExchange -> {
          //adds header to response
          serverWebExchange.getResponse().getHeaders().set("CUSTOM-RESPONSE-HEADER",
              HttpStatus.OK.equals(serverWebExchange.getResponse().getStatusCode()) ? "It worked": "It did not work");
          return serverWebExchange;
        })
        .then();
}