fix problem with missing '[' and ']' around IPv6 address. See https://tools.ietf.org/html/rfc7239
This commit is contained in:
committed by
Olga MaciaszekSharma
parent
c87602f09e
commit
8f8bc9e58a
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.gateway.filter.headers;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
@@ -120,8 +122,16 @@ public class ForwardedHeadersFilter implements HttpHeadersFilter, Ordered {
|
||||
if (remoteAddress != null) {
|
||||
// If remoteAddress is unresolved, calling getHostAddress() would cause a
|
||||
// NullPointerException.
|
||||
String forValue = remoteAddress.isUnresolved() ? remoteAddress.getHostName()
|
||||
: remoteAddress.getAddress().getHostAddress();
|
||||
String forValue;
|
||||
if (remoteAddress.isUnresolved()) {
|
||||
forValue = remoteAddress.getHostName();
|
||||
} else {
|
||||
InetAddress address = remoteAddress.getAddress();
|
||||
forValue = remoteAddress.getAddress().getHostAddress();
|
||||
if (address instanceof Inet6Address) {
|
||||
forValue = "[" + forValue + "]";
|
||||
}
|
||||
}
|
||||
int port = remoteAddress.getPort();
|
||||
if (port >= 0) {
|
||||
forValue = forValue + ":" + port;
|
||||
|
||||
@@ -131,6 +131,26 @@ public class ForwardedHeadersFilterTests {
|
||||
.containsEntry("for", "\"10.0.0.1:80\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correctIPv6RemoteAddressMapping() throws UnknownHostException {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get")
|
||||
.remoteAddress(new InetSocketAddress(InetAddress.getByName("2001:db8:cafe:0:0:0:0:17"), 80))
|
||||
.header(HttpHeaders.HOST, "myhost").build();
|
||||
|
||||
ForwardedHeadersFilter filter = new ForwardedHeadersFilter();
|
||||
|
||||
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
|
||||
|
||||
assertThat(headers.get(FORWARDED_HEADER)).hasSize(1);
|
||||
|
||||
List<Forwarded> forwardeds = ForwardedHeadersFilter.parse(headers.get(FORWARDED_HEADER));
|
||||
|
||||
assertThat(forwardeds).hasSize(1);
|
||||
Forwarded forwarded = forwardeds.get(0);
|
||||
|
||||
assertThat(forwarded.getValues()).containsEntry("for", "\"[2001:db8:cafe:0:0:0:0:17]:80\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unresolvedRemoteAddressFallsBackToHostName() throws UnknownHostException {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get")
|
||||
|
||||
Reference in New Issue
Block a user