-
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.
Update
RemoteAddrRoutePredicateFactory
to optionally respect the `X…
…-Forwarded-For` header. Fixes gh-155.
- Loading branch information
Showing
3 changed files
with
98 additions
and
7 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
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
56 changes: 56 additions & 0 deletions
56
...ava/org/springframework/cloud/gateway/handler/support/RoutePredicateFactoryUtilsTest.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,56 @@ | ||
package org.springframework.cloud.gateway.handler.support; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import org.junit.Test; | ||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
import org.springframework.mock.web.server.MockServerWebExchange; | ||
|
||
public class RoutePredicateFactoryUtilsTest { | ||
|
||
private InetSocketAddress getRemote0000Address() { | ||
try { | ||
return new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 1234); | ||
} | ||
catch (UnknownHostException e) { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
@Test | ||
public void parseRemoteIpPrioritizesFirstForwardedIp() { | ||
MockServerHttpRequest request = MockServerHttpRequest.get("someUrl") | ||
.remoteAddress(getRemote0000Address()) | ||
.header("X-Forwarded-For", "0.0.0.1, 0.0.0.2, 0.0.0.3").build(); | ||
MockServerWebExchange exchange = MockServerWebExchange.from(request); | ||
String actualIp = RoutePredicateFactoryUtils | ||
.parseRemoteIpRespectingForwardedHeader(exchange); | ||
|
||
assertThat(actualIp).isEqualTo("0.0.0.1"); | ||
} | ||
|
||
@Test | ||
public void parseRemoteIpFallsBackToRemoteIp() { | ||
MockServerHttpRequest request = MockServerHttpRequest.get("someUrl") | ||
.remoteAddress(getRemote0000Address()).build(); | ||
MockServerWebExchange exchange = MockServerWebExchange.from(request); | ||
String actualIp = RoutePredicateFactoryUtils | ||
.parseRemoteIpRespectingForwardedHeader(exchange); | ||
|
||
assertThat(actualIp).isEqualTo("0.0.0.0"); | ||
} | ||
|
||
@Test | ||
public void parseRemoteIpReturnsNullIfNoForwardedOrRemoteIp() { | ||
MockServerHttpRequest request = MockServerHttpRequest.get("someUrl").build(); | ||
MockServerWebExchange exchange = MockServerWebExchange.from(request); | ||
String actualIp = RoutePredicateFactoryUtils | ||
.parseRemoteIpRespectingForwardedHeader(exchange); | ||
|
||
assertThat(actualIp).isEqualTo(null); | ||
} | ||
} |