[BAEL-3311] Spring Cloud Gateway Routing Predicate Factories (#8549)

* [BAEL-3311] Spring Cloud Gateway Routing Predicate Factories

* [BAEL-3311] Code formatting

* [BAEL-3311] LiveTests
This commit is contained in:
psevestre
2020-01-21 13:05:53 -03:00
committed by maibin
parent 1a67e0a280
commit 24e69ed49a
8 changed files with 280 additions and 0 deletions
@@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.custompredicates;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class CustomPredicatesApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(CustomPredicatesApplication.class)
.profiles("customroutes")
.run(args);
}
}
@@ -0,0 +1,38 @@
package com.baeldung.springcloudgateway.custompredicates.config;
import org.springframework.cloud.gateway.filter.GatewayFilter;
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 com.baeldung.springcloudgateway.custompredicates.factories.GoldenCustomerRoutePredicateFactory;
import com.baeldung.springcloudgateway.custompredicates.factories.GoldenCustomerRoutePredicateFactory.Config;
import com.baeldung.springcloudgateway.custompredicates.service.GoldenCustomerService;
@Configuration
public class CustomPredicatesConfig {
@Bean
public GoldenCustomerRoutePredicateFactory goldenCustomer(GoldenCustomerService goldenCustomerService) {
return new GoldenCustomerRoutePredicateFactory(goldenCustomerService);
}
//@Bean
public RouteLocator routes(RouteLocatorBuilder builder, GoldenCustomerRoutePredicateFactory gf ) {
return builder.routes()
.route("dsl_golden_route", r -> r.path("/dsl_api/**")
.filters(f -> f.stripPrefix(1))
.uri("https://httpbin.org")
.predicate(gf.apply(new Config(true, "customerId"))))
.route("dsl_common_route", r -> r.path("/dsl_api/**")
.filters(f -> f.stripPrefix(1))
.uri("https://httpbin.org")
.predicate(gf.apply(new Config(false, "customerId"))))
.build();
}
}
@@ -0,0 +1,102 @@
/**
*
*/
package com.baeldung.springcloudgateway.custompredicates.factories;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import javax.validation.constraints.NotEmpty;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.http.HttpCookie;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;
import com.baeldung.springcloudgateway.custompredicates.service.GoldenCustomerService;
/**
* @author Philippe
*
*/
public class GoldenCustomerRoutePredicateFactory extends AbstractRoutePredicateFactory<GoldenCustomerRoutePredicateFactory.Config> {
private final GoldenCustomerService goldenCustomerService;
public GoldenCustomerRoutePredicateFactory(GoldenCustomerService goldenCustomerService ) {
super(Config.class);
this.goldenCustomerService = goldenCustomerService;
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("isGolden","customerIdCookie");
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return (ServerWebExchange t) -> {
List<HttpCookie> cookies = t.getRequest()
.getCookies()
.get(config.getCustomerIdCookie());
boolean isGolden;
if ( cookies == null || cookies.isEmpty()) {
isGolden = false;
}
else {
String customerId = cookies.get(0).getValue();
isGolden = goldenCustomerService.isGoldenCustomer(customerId);
}
return config.isGolden()?isGolden:!isGolden;
};
}
@Validated
public static class Config {
boolean isGolden = true;
@NotEmpty
String customerIdCookie = "customerId";
public Config() {}
public Config( boolean isGolden, String customerIdCookie) {
this.isGolden = isGolden;
this.customerIdCookie = customerIdCookie;
}
public boolean isGolden() {
return isGolden;
}
public void setGolden(boolean value) {
this.isGolden = value;
}
/**
* @return the customerIdCookie
*/
public String getCustomerIdCookie() {
return customerIdCookie;
}
/**
* @param customerIdCookie the customerIdCookie to set
*/
public void setCustomerIdCookie(String customerIdCookie) {
this.customerIdCookie = customerIdCookie;
}
}
}
@@ -0,0 +1,26 @@
/**
*
*/
package com.baeldung.springcloudgateway.custompredicates.service;
import org.springframework.stereotype.Component;
/**
* @author Philippe
*
*/
@Component
public class GoldenCustomerService {
public boolean isGoldenCustomer(String customerId) {
// TODO: Add some AI logic to check is this customer deserves a "golden" status ;^)
if ( "baeldung".equalsIgnoreCase(customerId)) {
return true;
}
else {
return false;
}
}
}