[BAEL-3313] spring-cloud/spring-cloud-gateway | Writing custom Spring Cloud Gateway Filters (#8182)
* updated dependency management in spring-cloud-gateway pom.xml * * renamed org package to com * renamed spring.cloud package to springcloudgateway * deleted SpringContextIntegrationTest, as per BAEL-14304 * updated spring Junit test to jupiter * separated introduction-application properties from application.yml, fixing launch error due to file not found exception * Added Service to use as Proxied Service * Added global filters and debug logs for article * fixed error in properties source, plus added GatewayFilter Factories * implemented Modify Request example * implemented Modify Response example * implemented Chain Request example * Added Tests: * Live Test for gateway * Integration tests for services Fixed small issues * renamed tests that were not following BDD naming
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.springcloudgateway.customfilters;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("classpath:customfilters-global-application.properties")
|
||||
public class CustomFiltersGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CustomFiltersGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfig {
|
||||
|
||||
@Bean
|
||||
WebClient client() {
|
||||
return WebClient.builder()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.factories;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale.LanguageRange;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class ChainRequestGatewayFilterFactory extends AbstractGatewayFilterFactory<ChainRequestGatewayFilterFactory.Config> {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(ChainRequestGatewayFilterFactory.class);
|
||||
|
||||
private final WebClient client;
|
||||
|
||||
public ChainRequestGatewayFilterFactory(WebClient client) {
|
||||
super(Config.class);
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> shortcutFieldOrder() {
|
||||
return Arrays.asList("endpoint", "defaultLanguage");
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
return client.get()
|
||||
.uri(config.getEndpoint())
|
||||
.exchange()
|
||||
.flatMap(response -> {
|
||||
return (response.statusCode()
|
||||
.is2xxSuccessful()) ? response.bodyToMono(String.class) : Mono.just(config.getDefaultLanguage());
|
||||
})
|
||||
.map(LanguageRange::parse)
|
||||
.map(range -> {
|
||||
exchange.getRequest()
|
||||
.mutate()
|
||||
.headers(h -> h.setAcceptLanguage(range))
|
||||
.build();
|
||||
|
||||
String allOutgoingRequestLanguages = exchange.getRequest()
|
||||
.getHeaders()
|
||||
.getAcceptLanguage()
|
||||
.stream()
|
||||
.map(r -> r.getRange())
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
logger.info("Chain Request output - Request contains Accept-Language header: " + allOutgoingRequestLanguages);
|
||||
|
||||
return exchange;
|
||||
})
|
||||
.flatMap(chain::filter);
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
private String endpoint;
|
||||
private String defaultLanguage;
|
||||
|
||||
public Config() {
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getDefaultLanguage() {
|
||||
return defaultLanguage;
|
||||
}
|
||||
|
||||
public void setDefaultLanguage(String defaultLanguage) {
|
||||
this.defaultLanguage = defaultLanguage;
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.factories;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class LoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<LoggingGatewayFilterFactory.Config> {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(LoggingGatewayFilterFactory.class);
|
||||
|
||||
public static final String BASE_MSG = "baseMessage";
|
||||
public static final String PRE_LOGGER = "preLogger";
|
||||
public static final String POST_LOGGER = "postLogger";
|
||||
|
||||
public LoggingGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> shortcutFieldOrder() {
|
||||
return Arrays.asList(BASE_MSG, PRE_LOGGER, POST_LOGGER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return new OrderedGatewayFilter((exchange, chain) -> {
|
||||
if (config.isPreLogger())
|
||||
logger.info("Pre GatewayFilter logging: " + config.getBaseMessage());
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
if (config.isPostLogger())
|
||||
logger.info("Post GatewayFilter logging: " + config.getBaseMessage());
|
||||
}));
|
||||
}, -2);
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
private String baseMessage;
|
||||
private boolean preLogger;
|
||||
private boolean postLogger;
|
||||
|
||||
public Config() {
|
||||
};
|
||||
|
||||
public Config(String baseMessage, boolean preLogger, boolean postLogger) {
|
||||
super();
|
||||
this.baseMessage = baseMessage;
|
||||
this.preLogger = preLogger;
|
||||
this.postLogger = postLogger;
|
||||
}
|
||||
|
||||
public String getBaseMessage() {
|
||||
return this.baseMessage;
|
||||
}
|
||||
|
||||
public boolean isPreLogger() {
|
||||
return preLogger;
|
||||
}
|
||||
|
||||
public boolean isPostLogger() {
|
||||
return postLogger;
|
||||
}
|
||||
|
||||
public void setBaseMessage(String baseMessage) {
|
||||
this.baseMessage = baseMessage;
|
||||
}
|
||||
|
||||
public void setPreLogger(boolean preLogger) {
|
||||
this.preLogger = preLogger;
|
||||
}
|
||||
|
||||
public void setPostLogger(boolean postLogger) {
|
||||
this.postLogger = postLogger;
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.factories;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ModifyRequestGatewayFilterFactory extends AbstractGatewayFilterFactory<ModifyRequestGatewayFilterFactory.Config> {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(ModifyRequestGatewayFilterFactory.class);
|
||||
|
||||
public ModifyRequestGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> shortcutFieldOrder() {
|
||||
return Arrays.asList("defaultLocale");
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
if (exchange.getRequest()
|
||||
.getHeaders()
|
||||
.getAcceptLanguage()
|
||||
.isEmpty()) {
|
||||
|
||||
String queryParamLocale = exchange.getRequest()
|
||||
.getQueryParams()
|
||||
.getFirst("locale");
|
||||
|
||||
Locale requestLocale = Optional.ofNullable(queryParamLocale)
|
||||
.map(l -> Locale.forLanguageTag(l))
|
||||
.orElse(config.getDefaultLocale());
|
||||
|
||||
exchange.getRequest()
|
||||
.mutate()
|
||||
.headers(h -> h.setAcceptLanguageAsLocales(Collections.singletonList(requestLocale)))
|
||||
.build();
|
||||
}
|
||||
|
||||
String allOutgoingRequestLanguages = exchange.getRequest()
|
||||
.getHeaders()
|
||||
.getAcceptLanguage()
|
||||
.stream()
|
||||
.map(range -> range.getRange())
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
logger.info("Modify Request output - Request contains Accept-Language header: " + allOutgoingRequestLanguages);
|
||||
return chain.filter(exchange);
|
||||
};
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
private Locale defaultLocale;
|
||||
|
||||
public Config() {
|
||||
}
|
||||
|
||||
public Locale getDefaultLocale() {
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
public void setDefaultLocale(String defaultLocale) {
|
||||
this.defaultLocale = Locale.forLanguageTag(defaultLocale);
|
||||
};
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.factories;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class ModifyResponseGatewayFilterFactory extends AbstractGatewayFilterFactory<ModifyResponseGatewayFilterFactory.Config> {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(ModifyResponseGatewayFilterFactory.class);
|
||||
|
||||
public ModifyResponseGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
|
||||
Optional.ofNullable(exchange.getRequest()
|
||||
.getQueryParams()
|
||||
.getFirst("locale"))
|
||||
.ifPresent(qp -> {
|
||||
String responseContentLanguage = response.getHeaders()
|
||||
.getContentLanguage()
|
||||
.getLanguage();
|
||||
|
||||
response.getHeaders()
|
||||
.add("Bael-Custom-Language-Header", responseContentLanguage);
|
||||
logger.info("Added custom header to Response");
|
||||
});
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.global;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Configuration
|
||||
public class LoggingGlobalFiltersConfigurations {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(LoggingGlobalFiltersConfigurations.class);
|
||||
|
||||
@Bean
|
||||
public GlobalFilter postGlobalFilter() {
|
||||
return (exchange, chain) -> {
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
logger.info("Global Post Filter executed");
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(-1)
|
||||
public GlobalFilter FirstPreLastPostGlobalFilter() {
|
||||
return (exchange, chain) -> {
|
||||
logger.info("First Pre Global Filter");
|
||||
return chain.filter(exchange)
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
logger.info("Last Post Global Filter");
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.filters.global;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class LoggingGlobalPreFilter implements GlobalFilter, Ordered {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(LoggingGlobalPreFilter.class);
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
logger.info("Global Pre Filter executed");
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springcloudgateway.customfilters.routes;
|
||||
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.baeldung.springcloudgateway.customfilters.filters.factories.LoggingGatewayFilterFactory;
|
||||
import com.baeldung.springcloudgateway.customfilters.filters.factories.LoggingGatewayFilterFactory.Config;
|
||||
|
||||
/**
|
||||
* Note: We want to keep this as an example of configuring a Route with a custom filter
|
||||
*
|
||||
* This corresponds with the properties configuration we have
|
||||
*/
|
||||
// @Configuration
|
||||
public class ServiceRouteConfiguration {
|
||||
|
||||
@Bean
|
||||
public RouteLocator routes(RouteLocatorBuilder builder, LoggingGatewayFilterFactory loggingFactory) {
|
||||
|
||||
return builder.routes()
|
||||
.route("service_route_java_config", r -> r.path("/service/**")
|
||||
.filters(f -> f.rewritePath("/service(?<segment>/?.*)", "$\\{segment}")
|
||||
.filter(loggingFactory.apply(new Config("My Custom Message", true, true))))
|
||||
.uri("http://localhost:8081"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.springcloudgateway.introduction;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("classpath:introduction-application.properties")
|
||||
public class IntroductionGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IntroductionGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user