開発者ガイド
これらは、ゲートウェイのいくつかのカスタムコンポーネントを作成するための基本的なガイドです。
カスタムルート述語ファクトリの作成
ルート述語を作成するには、RoutePredicateFactory
を Bean として実装する必要があります。拡張可能な AbstractRoutePredicateFactory
と呼ばれる抽象クラスがあります。
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
}
}
カスタム 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
}
}
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
}
}
構成でのカスタムフィルターと参照の命名
カスタムフィルターのクラス名は GatewayFilterFactory
で終わる必要があります。
例: 構成ファイルで Something
という名前のフィルターを参照するには、フィルターが SomethingGatewayFilterFactory
という名前のクラスに含まれている必要があります。
class AnotherThing など、GatewayFilterFactory サフィックスなしで名前が付けられたゲートウェイフィルターを作成することができます。このフィルターは、構成ファイルで 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
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();
}