19. 开发者指南

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

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

为了编写路径谓词,你需要实现路由谓词工厂作为一个豆子。有一个抽象类叫做摘要 路由谓词工厂你可以延长。spring-doc.cadn.net.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. 编写自定义网关Filter工厂

写成网关过滤器,你必须实现GatewayFilterFactory作为一个豆子。你可以扩展一个抽象类,叫做摘要网关滤器工厂. 以下示例展示了如何实现:spring-doc.cadn.net.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. 配置中命名自定义过滤器和引用

自定义过滤器的类名应以GatewayFilterFactory.spring-doc.cadn.net.cn

例如,引用一个名为东西在配置文件中,过滤器必须属于名为SomethingGatewayFilterFactory.spring-doc.cadn.net.cn

可以创建一个名为 的网关过滤器,但没有GatewayFilterFactory后缀,例如班级 另一个东西. 该Filter可以被引用为引用为另一个在配置文件中。这不是支持的命名惯例,该语法可能会在未来的版本中被移除。请更新过滤器名称以符合标准。

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

要编写自定义全局过滤器,必须实现全局Filter作为一个豆子接口。这对所有请求应用过滤器。spring-doc.cadn.net.cn

以下示例展示了如何分别设置全局预过滤和后处理过滤器:spring-doc.cadn.net.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();
}