Skip to content

Commit bfe1cb8

Browse files
committed
Adds support for trusted-proxies property.
spring.cloud.gateway.trusted-proxies
1 parent ba448ac commit bfe1cb8

File tree

14 files changed

+884
-93
lines changed

14 files changed

+884
-93
lines changed

docs/src/main/asciidoc/spring-cloud-gateway.adoc

Lines changed: 235 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ Route filters allow the modification of the incoming HTTP request or outgoing HT
489489
Route filters are scoped to a particular route.
490490
Spring Cloud Gateway includes many built-in GatewayFilter Factories.
491491

492-
NOTE: For more detailed examples of how to use any of the following filters, take a look at the https://github.com/spring-cloud/spring-cloud-gateway/tree/master/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory[unit tests].
492+
NOTE: For more detailed examples of how to use any of the following filters, take a look at the https://github.com/spring-cloud/spring-cloud-gateway/tree/3.1.x/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory[unit tests].
493493

494494
=== The `AddRequestHeader` `GatewayFilter` Factory
495495

@@ -620,31 +620,6 @@ spring:
620620
----
621621
====
622622

623-
=== The `DedupeResponseHeader` `GatewayFilter` Factory
624-
625-
The DedupeResponseHeader GatewayFilter factory takes a `name` parameter and an optional `strategy` parameter. `name` can contain a space-separated list of header names.
626-
The following example configures a `DedupeResponseHeader` `GatewayFilter`:
627-
628-
.application.yml
629-
====
630-
[source,yaml]
631-
----
632-
spring:
633-
cloud:
634-
gateway:
635-
routes:
636-
- id: dedupe_response_header_route
637-
uri: https://example.org
638-
filters:
639-
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
640-
----
641-
====
642-
643-
This removes duplicate values of `Access-Control-Allow-Credentials` and `Access-Control-Allow-Origin` response headers in cases when both the gateway CORS logic and the downstream logic add them.
644-
645-
The `DedupeResponseHeader` filter also accepts an optional `strategy` parameter.
646-
The accepted values are `RETAIN_FIRST` (default), `RETAIN_LAST`, and `RETAIN_UNIQUE`.
647-
648623
[[spring-cloud-circuitbreaker-filter-factory]]
649624
=== Spring Cloud CircuitBreaker GatewayFilter Factory
650625

@@ -802,6 +777,30 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
802777
----
803778
====
804779

780+
=== The `DedupeResponseHeader` `GatewayFilter` Factory
781+
782+
The `DedupeResponseHeader` GatewayFilter factory takes a `name` parameter and an optional `strategy` parameter. `name` can contain a space-separated list of header names.
783+
The following example configures a `DedupeResponseHeader` `GatewayFilter`:
784+
785+
.application.yml
786+
====
787+
[source,yaml]
788+
----
789+
spring:
790+
cloud:
791+
gateway:
792+
routes:
793+
- id: dedupe_response_header_route
794+
uri: https://example.org
795+
filters:
796+
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
797+
----
798+
====
799+
800+
This removes duplicate values of `Access-Control-Allow-Credentials` and `Access-Control-Allow-Origin` response headers in cases when both the gateway CORS logic and the downstream logic add them.
801+
802+
The `DedupeResponseHeader` filter also accepts an optional `strategy` parameter.
803+
The accepted values are `RETAIN_FIRST` (default), `RETAIN_LAST`, and `RETAIN_UNIQUE`.
805804

806805

807806
[[fallback-headers]]
@@ -849,6 +848,142 @@ You can overwrite the names of the headers in the configuration by setting the v
849848

850849
For more information on circuit breakers and the gateway see the <<spring-cloud-circuitbreaker-filter-factory, Spring Cloud CircuitBreaker Factory section>>.
851850

851+
=== The `JsonToGrpc` `GatewayFilter` Factory
852+
853+
The JSONToGRPCFilter GatewayFilter Factory converts a JSON payload to a gRPC request.
854+
855+
The filter takes the following arguments:
856+
857+
* `protoDescriptor`: Proto descriptor file.
858+
859+
This file can be generated using `protoc` and specifying the `--descriptor_set_out` flag:
860+
861+
[source,bash]
862+
----
863+
protoc --proto_path=src/main/resources/proto/ \
864+
--descriptor_set_out=src/main/resources/proto/hello.pb \
865+
src/main/resources/proto/hello.proto
866+
----
867+
868+
* `protoFile`: Proto definition file.
869+
870+
* `service`: Short name of the service that handles the request.
871+
872+
* `method`: Method name in the service that handles the request.
873+
874+
NOTE: `streaming` is not supported.
875+
876+
877+
*application.yml.*
878+
879+
[source,java]
880+
----
881+
@Bean
882+
public RouteLocator routes(RouteLocatorBuilder builder) {
883+
return builder.routes()
884+
.route("json-grpc", r -> r.path("/json/hello").filters(f -> {
885+
String protoDescriptor = "file:src/main/proto/hello.pb";
886+
String protoFile = "file:src/main/proto/hello.proto";
887+
String service = "HelloService";
888+
String method = "hello";
889+
return f.jsonToGRPC(protoDescriptor, protoFile, service, method);
890+
}).uri(uri))
891+
----
892+
893+
[source,yaml]
894+
----
895+
spring:
896+
cloud:
897+
gateway:
898+
routes:
899+
- id: json-grpc
900+
uri: https://localhost:6565/testhello
901+
predicates:
902+
- Path=/json/**
903+
filters:
904+
- name: JsonToGrpc
905+
args:
906+
protoDescriptor: file:proto/hello.pb
907+
protoFile: file:proto/hello.proto
908+
service: HelloService
909+
method: hello
910+
911+
----
912+
913+
When a request is made through the gateway to `/json/hello`, the request is transformed by using the definition provided in `hello.proto`, sent to `HelloService/hello`, and the response back is transformed to JSON.
914+
915+
By default, it creates a `NettyChannel` by using the default `TrustManagerFactory`. However, you can customize this `TrustManager` by creating a bean of type `GrpcSslConfigurer`:
916+
917+
[source,java]
918+
----
919+
920+
@Configuration
921+
public class GRPCLocalConfiguration {
922+
@Bean
923+
public GRPCSSLContext sslContext() {
924+
TrustManager trustManager = trustAllCerts();
925+
return new GRPCSSLContext(trustManager);
926+
}
927+
}
928+
----
929+
930+
[[local-cache-response-filter]]
931+
=== The `LocalResponseCache` `GatewayFilter` Factory
932+
933+
This filter allows caching the response body and headers to follow these rules:
934+
935+
* It can only cache bodiless GET requests.
936+
* It caches the response only for one of the following status codes: HTTP 200 (OK), HTTP 206 (Partial Content), or HTTP 301 (Moved Permanently).
937+
* Response data is not cached if `Cache-Control` header does not allow it (`no-store` present in the request or `no-store` or `private` present in the response).
938+
* If the response is already cached and a new request is performed with no-cache value in `Cache-Control` header, it returns a bodiless response with 304 (Not Modified).
939+
940+
This filter configures the local response cache per route and is available only if the `spring.cloud.gateway.filter.local-response-cache.enabled` property is enabled. And a <<local-cache-response-global-filter, local response cache configured globally>> is also available as feature.
941+
942+
It accepts the first parameter to override the time to expire a cache entry (expressed in `s` for seconds, `m` for minutes, and `h` for hours) and a second parameter to set the maximum size of the cache to evict entries for this route (`KB`, `MB`, or `GB`).
943+
944+
The following listing shows how to add local response cache `GatewayFilter`:
945+
946+
====
947+
[source,java]
948+
----
949+
@Bean
950+
public RouteLocator routes(RouteLocatorBuilder builder) {
951+
return builder.routes()
952+
.route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
953+
.filters(f -> f.prefixPath("/httpbin")
954+
.localResponseCache(Duration.ofMinutes(30), "500MB")
955+
).uri(uri))
956+
.build();
957+
}
958+
----
959+
960+
or this
961+
962+
.application.yaml
963+
[source,yaml]
964+
----
965+
spring:
966+
cloud:
967+
gateway:
968+
routes:
969+
- id: resource
970+
uri: http://localhost:9000
971+
predicates:
972+
- Path=/resource
973+
filters:
974+
- LocalResponseCache=30m,500MB
975+
----
976+
====
977+
978+
NOTE: This filter also automatically calculates the `max-age` value in the HTTP `Cache-Control` header.
979+
Only if `max-age` is present on the original response is the value rewritten with the number of seconds set in the `timeToLive` configuration parameter.
980+
In consecutive calls, this value is recalculated with the number of seconds left until the response expires.
981+
982+
NOTE: To enable this feature, add `com.github.ben-manes.caffeine:caffeine` and `spring-boot-starter-cache` as project dependencies.
983+
984+
WARNING: If your project creates custom `CacheManager` beans, it will either need to be marked with `@Primary` or injected using `@Qualifier`.
985+
986+
852987
=== The `MapRequestHeader` `GatewayFilter` Factory
853988

854989
The `MapRequestHeader` `GatewayFilter` factory takes `fromHeader` and `toHeader` parameters.
@@ -872,7 +1007,77 @@ spring:
8721007
----
8731008
====
8741009

875-
This adds `X-Request-Red:<values>` header to the downstream request with updated values from the incoming HTTP request's `Blue` header.
1010+
This adds the `X-Request-Red:<values>` header to the downstream request with updated values from the incoming HTTP request's `Blue` header.
1011+
1012+
=== The `ModifyRequestBody` `GatewayFilter` Factory
1013+
1014+
You can use the `ModifyRequestBody` filter to modify the request body before it is sent downstream by the gateway.
1015+
1016+
NOTE: This filter can be configured only by using the Java DSL.
1017+
1018+
The following listing shows how to modify a request body `GatewayFilter`:
1019+
1020+
====
1021+
[source,java]
1022+
----
1023+
@Bean
1024+
public RouteLocator routes(RouteLocatorBuilder builder) {
1025+
return builder.routes()
1026+
.route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
1027+
.filters(f -> f.prefixPath("/httpbin")
1028+
.modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
1029+
(exchange, s) -> Mono.just(new Hello(s.toUpperCase())))).uri(uri))
1030+
.build();
1031+
}
1032+
1033+
static class Hello {
1034+
String message;
1035+
1036+
public Hello() { }
1037+
1038+
public Hello(String message) {
1039+
this.message = message;
1040+
}
1041+
1042+
public String getMessage() {
1043+
return message;
1044+
}
1045+
1046+
public void setMessage(String message) {
1047+
this.message = message;
1048+
}
1049+
}
1050+
----
1051+
1052+
NOTE: If the request has no body, the `RewriteFilter` is passed `null`. `Mono.empty()` should be returned to assign a missing body in the request.
1053+
1054+
====
1055+
1056+
1057+
=== The `ModifyResponseBody` `GatewayFilter` Factory
1058+
1059+
You can use the `ModifyResponseBody` filter to modify the response body before it is sent back to the client.
1060+
1061+
NOTE: This filter can be configured only by using the Java DSL.
1062+
1063+
The following listing shows how to modify a response body `GatewayFilter`:
1064+
1065+
====
1066+
[source,java]
1067+
----
1068+
@Bean
1069+
public RouteLocator routes(RouteLocatorBuilder builder) {
1070+
return builder.routes()
1071+
.route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
1072+
.filters(f -> f.prefixPath("/httpbin")
1073+
.modifyResponseBody(String.class, String.class,
1074+
(exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri))
1075+
.build();
1076+
}
1077+
----
1078+
1079+
NOTE: If the response has no body, the `RewriteFilter` is passed `null`. `Mono.empty()` should be returned to assign a missing body in the response.
1080+
====
8761081

8771082
=== The `PrefixPath` `GatewayFilter` Factory
8781083

@@ -1094,7 +1299,7 @@ spring:
10941299

10951300
This removes the `X-Request-Foo` header before it is sent downstream.
10961301

1097-
=== `RemoveResponseHeader` `GatewayFilter` Factory
1302+
=== The `RemoveResponseHeader` `GatewayFilter` Factory
10981303

10991304
The `RemoveResponseHeader` `GatewayFilter` factory takes a `name` parameter.
11001305
It is the name of the header to be removed.
@@ -2100,7 +2305,7 @@ or check if an exchange has already been routed.
21002305
HttpHeadersFilters are applied to requests before sending them downstream, such as in the `NettyRoutingFilter`.
21012306

21022307
=== Forwarded Headers Filter
2103-
The `Forwarded` Headers Filter creates a `Forwarded` header to send to the downstream service. It adds the `Host` header, scheme and port of the current request to any existing `Forwarded` header.
2308+
The `Forwarded` Headers Filter creates a `Forwarded` header to send to the downstream service. It adds the `Host` header, scheme and port of the current request to any existing `Forwarded` header. To activate this filter set the `spring.cloud.gateway.trusted-proxies` property to a Java Regular Expression. This regular expression defines the proxies that are trusted when they appear in the `Forwarded` header.
21042309

21052310
=== RemoveHopByHop Headers Filter
21062311
The `RemoveHopByHop` Headers Filter removes headers from forwarded requests. The default list of headers that is removed comes from the https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3[IETF].
@@ -2118,7 +2323,7 @@ The `RemoveHopByHop` Headers Filter removes headers from forwarded requests. The
21182323
To change this, set the `spring.cloud.gateway.filter.remove-hop-by-hop.headers` property to the list of header names to remove.
21192324

21202325
=== XForwarded Headers Filter
2121-
The `XForwarded` Headers Filter creates various a `X-Forwarded-*` headers to send to the downstream service. It users the `Host` header, scheme, port and path of the current request to create the various headers.
2326+
The `XForwarded` Headers Filter creates various `X-Forwarded-*` headers to send to the downstream service. It uses the `Host` header, scheme, port and path of the current request to create the various headers. To activate this filter set the `spring.cloud.gateway.trusted-proxies` property to a Java Regular Expression. This regular expression defines the proxies that are trusted when they appear in the `X-Forwarded-For` header.
21222327

21232328
Creating of individual headers can be controlled by the following boolean properties (defaults to true):
21242329

0 commit comments

Comments
 (0)