Merge pull request #9159 from carloscaverobarca/BAEL-3312-Spring-Cloud-Gateway-WebFilter-Factories
BAEL-3312 Spring Cloud Gateway WebFilter Factories
This commit is contained in:
commit
fd2172c6c0
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-cloud-gateway</artifactId>
|
||||
<name>spring-cloud-gateway</name>
|
||||
|
@ -49,6 +49,26 @@
|
|||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Circuit Breaker -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Embedded Redis -->
|
||||
<dependency>
|
||||
<groupId>it.ozimov</groupId>
|
||||
<artifactId>embedded-redis</artifactId>
|
||||
<version>${redis.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator-cdi</artifactId>
|
||||
|
@ -69,8 +89,8 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -84,11 +104,10 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-cloud-dependencies.version>Greenwich.SR3</spring-cloud-dependencies.version>
|
||||
|
||||
<!-- Spring Boot version compatible with Spring Cloud Release train -->
|
||||
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
|
||||
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
|
||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||
<redis.version>0.7.2</redis.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.springcloudgateway.webfilters;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebFilterGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(WebFilterGatewayApplication.class)
|
||||
.profiles("webfilters")
|
||||
.run(args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.springcloudgateway.webfilters.config;
|
||||
|
||||
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.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Configuration
|
||||
public class ModifyBodyRouteConfig {
|
||||
|
||||
@Bean
|
||||
public RouteLocator routes(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("modify_request_body", r -> r.path("/post")
|
||||
.filters(f -> f.modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
|
||||
(exchange, s) -> Mono.just(new Hello(s.toUpperCase())))).uri("https://httpbin.org"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouteLocator responseRoutes(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("modify_response_body", r -> r.path("/put/**")
|
||||
.filters(f -> f.modifyResponseBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
|
||||
(exchange, s) -> Mono.just(new Hello("New Body")))).uri("https://httpbin.org"))
|
||||
.build();
|
||||
}
|
||||
|
||||
static class Hello {
|
||||
String message;
|
||||
|
||||
public Hello() { }
|
||||
|
||||
public Hello(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.springcloudgateway.webfilters.config;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Configuration
|
||||
public class RequestRateLimiterResolverConfig {
|
||||
|
||||
@Bean
|
||||
KeyResolver userKeyResolver() {
|
||||
return exchange -> Mono.just("1");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: INFO
|
||||
reactor.netty.http.client: INFO
|
||||
|
||||
spring:
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
cloud:
|
||||
gateway:
|
||||
routes:
|
||||
- id: request_header_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/get/**
|
||||
filters:
|
||||
- AddRequestHeader=My-Header-Good,Good
|
||||
- AddRequestHeader=My-Header-Remove,Remove
|
||||
- AddRequestParameter=var, good
|
||||
- AddRequestParameter=var2, remove
|
||||
- MapRequestHeader=My-Header-Good, My-Header-Bad
|
||||
- MapRequestHeader=My-Header-Set, My-Header-Bad
|
||||
- SetRequestHeader=My-Header-Set, Set
|
||||
- RemoveRequestHeader=My-Header-Remove
|
||||
- RemoveRequestParameter=var2
|
||||
- PreserveHostHeader
|
||||
|
||||
- id: response_header_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/header/post/**
|
||||
filters:
|
||||
- AddResponseHeader=My-Header-Good,Good
|
||||
- AddResponseHeader=My-Header-Set,Good
|
||||
- AddResponseHeader=My-Header-Rewrite, password=12345678
|
||||
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
|
||||
- AddResponseHeader=My-Header-Remove,Remove
|
||||
- SetResponseHeader=My-Header-Set, Set
|
||||
- RemoveResponseHeader=My-Header-Remove
|
||||
- RewriteResponseHeader=My-Header-Rewrite, password=[^&]+, password=***
|
||||
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
|
||||
- StripPrefix=1
|
||||
|
||||
- id: path_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/new/post/**
|
||||
filters:
|
||||
- RewritePath=/new(?<segment>/?.*), $\{segment}
|
||||
- SetPath=/post
|
||||
|
||||
- id: redirect_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/fake/post/**
|
||||
filters:
|
||||
- RedirectTo=302, https://httpbin.org
|
||||
|
||||
- id: status_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/delete/**
|
||||
filters:
|
||||
- SetStatus=401
|
||||
|
||||
- id: size_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/anything
|
||||
filters:
|
||||
- name: RequestSize
|
||||
args:
|
||||
maxSize: 5000000
|
||||
|
||||
- id: retry_test
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/status/502
|
||||
filters:
|
||||
- name: Retry
|
||||
args:
|
||||
retries: 3
|
||||
statuses: BAD_GATEWAY
|
||||
methods: GET,POST
|
||||
backoff:
|
||||
firstBackoff: 10ms
|
||||
maxBackoff: 50ms
|
||||
factor: 2
|
||||
basedOnPreviousValue: false
|
||||
|
||||
- id: request_rate_limiter
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/redis/get/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- name: RequestRateLimiter
|
||||
args:
|
||||
redis-rate-limiter.replenishRate: 10
|
||||
redis-rate-limiter.burstCapacity: 5
|
||||
key-resolver: "#{@userKeyResolver}"
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.springcloudgateway.webfilters;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("webfilters")
|
||||
@TestConfiguration
|
||||
public class RedisWebFilterFactoriesLiveTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RedisWebFilterFactoriesLiveTest.class);
|
||||
|
||||
private RedisServer redisServer;
|
||||
|
||||
public RedisWebFilterFactoriesLiveTest() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void postConstruct() {
|
||||
this.redisServer = new RedisServer(6379);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@LocalServerPort
|
||||
String port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate template;
|
||||
|
||||
@RepeatedTest(25)
|
||||
public void whenCallRedisGetThroughGateway_thenOKStatusOrIsReceived() {
|
||||
String url = "http://localhost:" + port + "/redis/get";
|
||||
|
||||
ResponseEntity<String> r = restTemplate.getForEntity(url, String.class);
|
||||
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
LOGGER.info("Received: status->{}, reason->{}, remaining->{}",
|
||||
r.getStatusCodeValue(), r.getStatusCode().getReasonPhrase(),
|
||||
r.getHeaders().get("X-RateLimit-Remaining"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void preDestroy() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
package com.baeldung.springcloudgateway.webfilters;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
|
||||
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("webfilters")
|
||||
public class WebFilterFactoriesLiveTest {
|
||||
|
||||
@LocalServerPort
|
||||
String port;
|
||||
|
||||
@Autowired
|
||||
private WebTestClient client;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@BeforeEach
|
||||
public void configureClient() {
|
||||
client = WebTestClient.bindToServer()
|
||||
.baseUrl("http://localhost:" + port)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallGetThroughGateway_thenAllHTTPRequestHeadersParametersAreSet() throws JSONException {
|
||||
String url = "http://localhost:" + port + "/get";
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
JSONObject json = new JSONObject(response.getBody());
|
||||
JSONObject headers = json.getJSONObject("headers");
|
||||
assertThat(headers.getString("My-Header-Good")).isEqualTo("Good");
|
||||
assertThat(headers.getString("My-Header-Bad")).isEqualTo("Good");
|
||||
assertThat(headers.getString("My-Header-Set")).isEqualTo("Set");
|
||||
assertTrue(headers.isNull("My-Header-Remove"));
|
||||
JSONObject vars = json.getJSONObject("args");
|
||||
assertThat(vars.getString("var")).isEqualTo("good");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallHeaderPostThroughGateway_thenAllHTTPResponseHeadersAreSet() {
|
||||
ResponseSpec response = client.post()
|
||||
.uri("/header/post")
|
||||
.exchange();
|
||||
|
||||
response.expectStatus()
|
||||
.isOk()
|
||||
.expectHeader()
|
||||
.valueEquals("My-Header-Rewrite", "password=***")
|
||||
.expectHeader()
|
||||
.valueEquals("My-Header-Set", "Set")
|
||||
.expectHeader()
|
||||
.valueEquals("My-Header-Good", "Good")
|
||||
.expectHeader()
|
||||
.doesNotExist("My-Header-Remove");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallPostThroughGateway_thenBodyIsRetrieved() throws JSONException {
|
||||
String url = "http://localhost:" + port + "/post";
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>("content", new HttpHeaders());
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
JSONObject json = new JSONObject(response.getBody());
|
||||
JSONObject data = json.getJSONObject("json");
|
||||
assertThat(data.getString("message")).isEqualTo("CONTENT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallPutThroughGateway_thenBodyIsRetrieved() throws JSONException {
|
||||
String url = "http://localhost:" + port + "/put";
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>("CONTENT", new HttpHeaders());
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
JSONObject json = new JSONObject(response.getBody());
|
||||
assertThat(json.getString("message")).isEqualTo("New Body");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallDeleteThroughGateway_thenIsUnauthorizedCodeIsSet() {
|
||||
ResponseSpec response = client.delete()
|
||||
.uri("/delete")
|
||||
.exchange();
|
||||
|
||||
response.expectStatus()
|
||||
.isUnauthorized();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallFakePostThroughGateway_thenIsUnauthorizedCodeIsSet() {
|
||||
ResponseSpec response = client.post()
|
||||
.uri("/fake/post")
|
||||
.exchange();
|
||||
|
||||
response.expectStatus()
|
||||
.is3xxRedirection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallStatus504ThroughGateway_thenCircuitBreakerIsExecuted() throws JSONException {
|
||||
String url = "http://localhost:" + port + "/status/504";
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
|
||||
JSONObject json = new JSONObject(response.getBody());
|
||||
assertThat(json.getString("url")).contains("anything");
|
||||
}
|
||||
}
|
|
@ -3,7 +3,15 @@
|
|||
<appender name="LISTAPPENDER"
|
||||
class="com.baeldung.springcloudgateway.customfilters.gatewayapp.utils.LoggerListAppender">
|
||||
</appender>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="info">
|
||||
<appender-ref ref="LISTAPPENDER" />
|
||||
</root>
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
Loading…
Reference in New Issue