Add WebFilter Factories examples configured through yaml and Java DSL
This commit is contained in:
parent
62bf6e13f9
commit
b37ee5f26d
|
@ -49,6 +49,12 @@
|
|||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Circuit Breaker -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator-cdi</artifactId>
|
||||
|
@ -84,10 +90,8 @@
|
|||
</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>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -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,93 @@
|
|||
spring:
|
||||
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: circuitbreaker_route
|
||||
uri: https://httpbin.org
|
||||
predicates:
|
||||
- Path=/status/504
|
||||
filters:
|
||||
- name: CircuitBreaker
|
||||
args:
|
||||
name: myCircuitBreaker
|
||||
fallbackUri: forward:/anything
|
||||
- RewritePath=/status/504, /anything
|
Loading…
Reference in New Issue