19. 开发者指南
这些是编写网关的一些自定义组件的基本指南。
19.1. 写自定义路由断言工厂
要编写路线断言,您必须实现 0 作为bean。您可以扩展名为 1 的抽象类。
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,必须将GatewayFilterFactory实现为bean。
可以扩展一个名为AbstractGatewayFilterFactory的抽象类。
下面的示例展示了如何执行此操作:
示例 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结尾。
例如,要在配置文件中引用名为 Something 的过滤器,该过滤器必须在名为 SomethingGatewayFilterFactory 的类中。
它有可能创建一个网关过滤器名为没有尾随的GatewayFilterFactory后缀,比如说class AnotherThing。这个过滤器可以被引用为配置文件中的AnotherThing。这不是一个支持的命名约定,这种语法可能会在以后的版本中删除。请更新过滤器名以符合规定。 |
19.3. 编写自定义全局过滤器
要编写自定义全局筛选器,您必须实现GlobalFilter接口,并将其作为bean进行注册。此筛选器适用于所有请求。
以下示例展示了如何设置全局预过滤器和后过滤器,分别如下:
@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();
}