17. 用户指南
这些是编写网关的一些自定义组件的基本指南。
17.1. 编写自定义路由谓词工厂
要编写路由断言,您需要实现RoutePredicateFactory。您可以扩展名为AbstractRoutePredicateFactory的抽象类。
MyRoutePredicateFactory.java
public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.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
}
}
17.2. 写出定制的网关过滤器工厂
要编写一个 GatewayFilter,必须实现 GatewayFilterFactory。
你可以扩展一个名为 AbstractGatewayFilterFactory 的抽象类。
以下示例展示了如何操作:
示例 72。PreGatewayFilterFactory.java
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
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
}
}
17.2.1. 在配置中为自定义过滤器和引用命名
自定义过滤器类名应以GatewayFilterFactory结尾。
例如,要在配置文件中引用名为 Something 的过滤器,该过滤器必须在名为 SomethingGatewayFilterFactory 的类中。
它有可能创建一个网关过滤器名为没有尾随的GatewayFilterFactory后缀,比如说class AnotherThing。这个过滤器可以被引用为配置文件中的AnotherThing。这不是一个支持的命名约定,这种语法可能会在以后的版本中删除。请更新过滤器名以符合规定。 |
17.3. 编写自定义全局过滤器
要编写自定义全局过滤器,您必须实现GlobalFilter接口。此操作会将过滤器应用于所有请求。
以下示例展示了如何设置全局预过滤器和后过滤器,分别如下:
@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();
}