[BAEL-3311] Spring Cloud Gateway Routing Predicate Factories
This commit is contained in:
parent
ee96016774
commit
3d6d0765a3
@ -68,6 +68,10 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.springcloudgateway.custompredicates;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class CustomRoutersGatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new SpringApplicationBuilder(CustomRoutersGatewayApplication.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,104 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
routes:
|
||||||
|
- id: golden_route
|
||||||
|
uri: https://httpbin.org
|
||||||
|
predicates:
|
||||||
|
- Path=/api/**
|
||||||
|
- GoldenCustomer=true
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- AddRequestHeader=GoldenCustomer,true
|
||||||
|
- id: common_route
|
||||||
|
uri: https://httpbin.org
|
||||||
|
predicates:
|
||||||
|
- Path=/api/**
|
||||||
|
- name: GoldenCustomer
|
||||||
|
args:
|
||||||
|
golden: false
|
||||||
|
customerIdCookie: customerId
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- AddRequestHeader=GoldenCustomer,false
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user