开发者指南
以下是编写网关自定义组件的基础指南。
编写自定义路由断言工厂
为了编写路由断言,你需要将 RoutePredicateFactory 实现为一个 Bean。有一个名为 AbstractRoutePredicateFactory 的抽象类,你可以继承它。
@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
}
}
编写自定义 GatewayFilter 工厂
要编写一个 GatewayFilter,你必须将 GatewayFilterFactory 实现为一个 bean。你可以扩展一个名为 AbstractGatewayFilterFactory 的抽象类。以下示例展示了如何操作:
示例 1. 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
}
}
@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
}
}
在配置中命名自定义过滤器和引用
自定义过滤器类的名称应以 GatewayFilterFactory 结尾。
例如,要在配置文件中引用名为 Something 的过滤器,该过滤器必须位于名为 SomethingGatewayFilterFactory 的类中。
注意
可以创建一个不以 GatewayFilterFactory 结尾命名的网关过滤器,例如 class AnotherThing。在配置文件中,该过滤器可以被引用为 AnotherThing。这不是一个受支持的命名约定,并且此语法可能在未来的版本中被移除。请更新过滤器名称以符合规范。
编写自定义全局过滤器
要编写自定义全局过滤器,必须将 GlobalFilter 接口实现为一个 bean。这会将过滤器应用于所有请求。
以下示例分别展示了如何设置全局前置和后置过滤器:
@Bean
public GlobalFilter customGlobalFilter() {
return (exchange, chain) -> exchange.getPrincipal()
.map(Principal::getName)
.defaultIfEmpty("Default User")
.map(userName -> {
//adds header to proxied request
ServerHttpRequest.Builder builder = exchange.getRequest().mutate().header("CUSTOM-REQUEST-HEADER", userName);
//use builder to manipulate the request
return exchange.mutate().request(builder.build()).build();
})
.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();
}