Move rate limiting examples
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.springcloudgateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication()
|
||||
@PropertySource("classpath:ipaddress-application.properties")
|
||||
public class IpAddressApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IpAddressApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("requestratelimiter_route", p -> p
|
||||
.path("/example")
|
||||
.filters(f -> f.requestRateLimiter(r -> r.setRateLimiter(redisRateLimiter())))
|
||||
.uri("http://example.org"))
|
||||
.route("ipaddress_route", p -> p
|
||||
.path("/example2")
|
||||
.filters(f -> f.requestRateLimiter(r -> r.setRateLimiter(redisRateLimiter())
|
||||
.setDenyEmptyKey(false)
|
||||
.setKeyResolver(new SimpleClientAddressResolver())))
|
||||
.uri("http://example.org"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisRateLimiter redisRateLimiter() {
|
||||
return new RedisRateLimiter(1, 1, 1);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.cloud.gateway.support.ipresolver.XForwardedRemoteAddressResolver;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@Primary
|
||||
@Component
|
||||
public class ProxiedClientAddressResolver implements KeyResolver {
|
||||
@Override
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
XForwardedRemoteAddressResolver resolver = XForwardedRemoteAddressResolver.maxTrustedIndex(1);
|
||||
InetSocketAddress inetSocketAddress = resolver.resolve(exchange);
|
||||
return Mono.just(inetSocketAddress.getAddress().getHostAddress());
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class SimpleClientAddressResolver implements KeyResolver {
|
||||
@Override
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
return Optional.ofNullable(exchange.getRequest().getRemoteAddress())
|
||||
.map(InetSocketAddress::getAddress)
|
||||
.map(InetAddress::getHostAddress)
|
||||
.map(Mono::just)
|
||||
.orElse(Mono.empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user