-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e967d23
commit 2995005
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...java/org/springframework/cloud/gateway/filter/route/RemoveNonProxyHeadersRouteFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.springframework.cloud.gateway.filter.route; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.web.server.WebFilter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Hop-by-hop header fields, which are meaningful only for a single transport-level connection, | ||
* and are not stored by caches or forwarded by proxies. The following HTTP/1.1 header fields | ||
* are hop-by-hop header fields: | ||
* <ul> | ||
* <li>Connection | ||
* <li>Keep-Alive | ||
* <li>Proxy-Authenticate | ||
* <li>Proxy-Authorization | ||
* <li>TE | ||
* <li>Trailer | ||
* <li>Transfer-Encoding | ||
* <li>Upgrade | ||
* </ul> | ||
* | ||
* See https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3 | ||
* | ||
* @author Spencer Gibb | ||
*/ | ||
@ConfigurationProperties("spring.cloud.gateway.filter.removeNonProxyHeaders") | ||
public class RemoveNonProxyHeadersRouteFilter implements RouteFilter { | ||
|
||
private static final String FAKE_HEADER = "_______force_______"; | ||
public static final String[] DEFAULT_HEADERS_TO_REMOVE = new String[] {"Connection", "Keep-Alive", | ||
"Proxy-Authenticate", "Proxy-Authorization", "TE", "Trailer", "Transfer-Encoding", "Upgrade"}; | ||
|
||
private List<String> headers = Arrays.asList(DEFAULT_HEADERS_TO_REMOVE); | ||
|
||
public List<String> getHeaders() { | ||
return headers; | ||
} | ||
|
||
public void setHeaders(List<String> headers) { | ||
this.headers = headers; | ||
} | ||
|
||
@Override | ||
public WebFilter apply(String... args) { | ||
//TODO: support filter args | ||
|
||
return (exchange, chain) -> { | ||
ServerHttpRequest request = exchange.getRequest().mutate() | ||
.header(FAKE_HEADER, "mutable") //TODO: is there a better way? | ||
.build(); | ||
|
||
request.getHeaders().remove(FAKE_HEADER); | ||
|
||
for (String header : this.headers) { | ||
request.getHeaders().remove(header); | ||
} | ||
|
||
return chain.filter(exchange.mutate().request(request).build()); | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/springframework/cloud/gateway/support/NameUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.springframework.cloud.gateway.support; | ||
|
||
import org.springframework.cloud.gateway.filter.route.RouteFilter; | ||
import org.springframework.cloud.gateway.handler.predicate.RoutePredicate; | ||
|
||
/** | ||
* @author Spencer Gibb | ||
*/ | ||
public class NameUtils { | ||
|
||
public static String normalizePredicateName(Class<? extends RoutePredicate> clazz) { | ||
return clazz.getSimpleName().replace(RoutePredicate.class.getSimpleName(), ""); | ||
} | ||
|
||
public static String normalizeFilterName(Class<? extends RouteFilter> clazz) { | ||
return clazz.getSimpleName().replace(RouteFilter.class.getSimpleName(), ""); | ||
} | ||
} |