Browse Source

Adds RemoteAddrRoutePredicate

RemoteAddr=127.0.0.1/24
pull/41/head
Spencer Gibb 8 years ago
parent
commit
c31e92a2b5
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 5
      pom.xml
  2. 7
      src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  3. 66
      src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRoutePredicate.java
  4. 6
      src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java
  5. 1
      src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java
  6. 1
      src/test/resources/application.yml

5
pom.xml

@ -81,6 +81,11 @@ @@ -81,6 +81,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>

7
src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@ -40,6 +40,7 @@ import org.springframework.cloud.gateway.handler.predicate.HeaderRoutePredicate; @@ -40,6 +40,7 @@ import org.springframework.cloud.gateway.handler.predicate.HeaderRoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.HostRoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.MethodRoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.QueryRoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.RemoteAddrRoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.RoutePredicate;
import org.springframework.cloud.gateway.handler.predicate.UrlRoutePredicate;
import org.springframework.context.annotation.Bean;
@ -163,6 +164,12 @@ public class GatewayAutoConfiguration { @@ -163,6 +164,12 @@ public class GatewayAutoConfiguration {
return new QueryRoutePredicate();
}
@Bean(name = "RemoteAddrRoutePredicate")
public RemoteAddrRoutePredicate remoteAddrRoutePredicate() {
return new RemoteAddrRoutePredicate();
}
@Bean(name = "UrlRoutePredicate")
public UrlRoutePredicate urlRoutePredicate() {
return new UrlRoutePredicate();

66
src/main/java/org/springframework/cloud/gateway/handler/predicate/RemoteAddrRoutePredicate.java

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
package org.springframework.cloud.gateway.handler.predicate;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.util.SubnetUtils;
import org.springframework.http.server.reactive.ReactorServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
/**
* @author Spencer Gibb
*/
public class RemoteAddrRoutePredicate implements RoutePredicate {
private static final Log log = LogFactory.getLog(RemoteAddrRoutePredicate.class);
@Override
public Predicate<ServerWebExchange> apply(String source, String[] args) {
List<SubnetUtils> sources = new ArrayList<>();
addSource(sources, source);
if (args != null) {
for (String arg : args) {
addSource(sources, arg);
}
}
return exchange -> {
ReactorServerHttpRequest request = (ReactorServerHttpRequest) exchange.getRequest();
InetSocketAddress remoteAddress = request.getReactorRequest().remoteAddress();
String hostAddress = remoteAddress.getAddress().getHostAddress();
String host = exchange.getRequest().getURI().getHost();
if (!hostAddress.equals(host)) {
log.warn("Remote addresses didn't match " + hostAddress + " != " + host);
}
for (SubnetUtils subnet : sources) {
if (subnet.getInfo().isInRange(hostAddress)) {
return true;
}
}
return false;
};
}
private void addSource(List<SubnetUtils> sources, String source) {
boolean inclusiveHostCount = false;
if (!source.contains("/")) { // no netmask, add default
source = source + "/32";
}
if (source.endsWith("/32")) {
//http://stackoverflow.com/questions/2942299/converting-cidr-address-to-subnet-mask-and-network-address#answer-6858429
inclusiveHostCount = true;
}
//TODO: howto support ipv6 as well?
SubnetUtils subnetUtils = new SubnetUtils(source);
subnetUtils.setInclusiveHostCount(inclusiveHostCount);
sources.add(subnetUtils);
}
}

6
src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

@ -43,7 +43,11 @@ public class GatewayIntegrationTests { @@ -43,7 +43,11 @@ public class GatewayIntegrationTests {
private static final String HANDLER_MAPPER_HEADER = "X-Gateway-Handler-Mapper-Class";
private static final String ROUTE_ID_HEADER = "X-Gateway-Route-Id";
public static final Duration DURATION = Duration.ofSeconds(5);
private static final Duration DURATION = Duration.ofSeconds(5);
static {
System.setProperty("java.net.preferIPv4Stack", "true");
}
@LocalServerPort
private int port;

1
src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java

@ -53,6 +53,7 @@ public class GatewayTestApplication { @@ -53,6 +53,7 @@ public class GatewayTestApplication {
}
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true"); //Remove when configurable
SpringApplication.run(GatewayTestApplication.class, args);
}
}

1
src/test/resources/application.yml

@ -140,6 +140,7 @@ spring: @@ -140,6 +140,7 @@ spring:
uri: http://httpbin.org:80
predicates:
- Host=**.baz.org
- RemoteAddr=127.0.0.1/24
filters:
# $\ is being used as an escape
- RewritePath=/foo/(?<segment>.*), /$\{segment}

Loading…
Cancel
Save