JAVA-11240 Moved spring-cloud to spring-cloud-modules

This commit is contained in:
Dhawal Kapil
2022-05-29 19:32:17 +05:30
parent a7cae2e766
commit 85d134952e
934 changed files with 35 additions and 230 deletions
@@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp;
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);
}
}
@@ -0,0 +1,16 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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();
}
}
@@ -0,0 +1,89 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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("languageServiceEndpoint", "defaultLanguage");
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
return client.get()
.uri(config.getLanguageServiceEndpoint())
.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));
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 languageServiceEndpoint;
private String defaultLanguage;
public Config() {
}
public String getLanguageServiceEndpoint() {
return languageServiceEndpoint;
}
public void setLanguageServiceEndpoint(String languageServiceEndpoint) {
this.languageServiceEndpoint = languageServiceEndpoint;
}
public String getDefaultLanguage() {
return defaultLanguage;
}
public void setDefaultLanguage(String defaultLanguage) {
this.defaultLanguage = defaultLanguage;
}
}
}
@@ -0,0 +1,85 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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());
}));
}, 1);
}
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;
}
}
}
@@ -0,0 +1,92 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
@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)));
}
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);
ServerWebExchange modifiedExchange = exchange.mutate()
.request(originalRequest -> originalRequest.uri(UriComponentsBuilder.fromUri(exchange.getRequest()
.getURI())
.replaceQueryParams(new LinkedMultiValueMap<String, String>())
.build()
.toUri()))
.build();
logger.info("Removed all query params: {}", modifiedExchange.getRequest()
.getURI());
return chain.filter(modifiedExchange);
};
}
public static class Config {
private Locale defaultLocale;
public Config() {
}
public Locale getDefaultLocale() {
return defaultLocale;
}
public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = Locale.forLanguageTag(defaultLocale);
};
}
}
@@ -0,0 +1,48 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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 {
}
}
@@ -0,0 +1,31 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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 FirstPreLastPostGlobalFilter implements GlobalFilter, Ordered {
final Logger logger = LoggerFactory.getLogger(FirstPreLastPostGlobalFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
logger.info("First Pre Global Filter");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
logger.info("Last Post Global Filter");
}));
}
@Override
public int getOrder() {
return -1;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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 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");
}));
};
}
}
@@ -0,0 +1,22 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class LoggingGlobalPreFilter implements GlobalFilter {
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);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.springcloudgateway.customfilters.gatewayapp.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.gatewayapp.filters.factories.LoggingGatewayFilterFactory;
import com.baeldung.springcloudgateway.customfilters.gatewayapp.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();
}
}
@@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.customfilters.secondservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:secondservice-application.properties")
public class SecondServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SecondServiceApplication.class, args);
}
}
@@ -0,0 +1,18 @@
package com.baeldung.springcloudgateway.customfilters.secondservice.web;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class SecondServiceRestController {
@GetMapping("/resource/language")
public Mono<ResponseEntity<String>> getResource() {
return Mono.just(ResponseEntity.ok()
.body("es"));
}
}
@@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.customfilters.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:service-application.properties")
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.springcloudgateway.customfilters.service.web;
import java.util.Locale;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class ServiceRestController {
@GetMapping("/resource")
public Mono<ResponseEntity<String>> getResource() {
return Mono.just(ResponseEntity.ok()
.header(HttpHeaders.CONTENT_LANGUAGE, Locale.ENGLISH.getLanguage())
.body("Service Resource"));
}
}
@@ -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,43 @@
package com.baeldung.springcloudgateway.custompredicates.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 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.predicate(gf.apply(new Config(true, "customerId")))
.and()
.path("/dsl_api/**")
.filters(f -> f.stripPrefix(1))
.uri("https://httpbin.org")
)
.route("dsl_common_route", r ->
r.predicate(gf.apply(new Config(false, "customerId")))
.and()
.path("/dsl_api/**")
.filters(f -> f.stripPrefix(1))
.uri("https://httpbin.org")
)
.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;
}
}
}
@@ -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);
}
}
@@ -0,0 +1,44 @@
/**
*
*/
package com.baeldung.springcloudgateway.oauth.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.oauth2.server.resource.introspection.NimbusReactiveOpaqueTokenIntrospector;
import org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector;
import com.baeldung.springcloudgateway.oauth.shared.KeycloakReactiveTokenInstrospector;
/**
* @author Philippe
*
*/
@SpringBootApplication
@PropertySource("classpath:quotes-application.properties")
@EnableWebFluxSecurity
public class QuotesApplication {
public static void main(String[] args) {
SpringApplication.run(QuotesApplication.class);
}
@Bean
public ReactiveOpaqueTokenIntrospector keycloakIntrospector(OAuth2ResourceServerProperties props) {
NimbusReactiveOpaqueTokenIntrospector delegate = new NimbusReactiveOpaqueTokenIntrospector(
props.getOpaquetoken().getIntrospectionUri(),
props.getOpaquetoken().getClientId(),
props.getOpaquetoken().getClientSecret());
return new KeycloakReactiveTokenInstrospector(delegate);
}
}
@@ -0,0 +1,35 @@
package com.baeldung.springcloudgateway.oauth.backend.domain;
public class Quote {
private String symbol;
private double price;
/**
* @return the symbol
*/
public String getSymbol() {
return symbol;
}
/**
* @param symbol the symbol to set
*/
public void setSymbol(String symbol) {
this.symbol = symbol;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.springcloudgateway.oauth.backend.web;
import javax.annotation.PostConstruct;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springcloudgateway.oauth.backend.domain.Quote;
import reactor.core.publisher.Mono;
@RestController
public class QuoteApi {
private static final GrantedAuthority GOLD_CUSTOMER = new SimpleGrantedAuthority("gold");
@GetMapping("/quotes/{symbol}")
public Mono<Quote> getQuote(@PathVariable("symbol") String symbol, BearerTokenAuthentication auth ) {
Quote q = new Quote();
q.setSymbol(symbol);
if ( auth.getAuthorities().contains(GOLD_CUSTOMER)) {
q.setPrice(10.0);
}
else {
q.setPrice(12.0);
}
return Mono.just(q);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.springcloudgateway.oauth.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
@SpringBootApplication
@EnableWebFluxSecurity
public class ResourceServerGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceServerGatewayApplication.class,args);
}
}
@@ -0,0 +1,65 @@
/**
*
*/
package com.baeldung.springcloudgateway.oauth.shared;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector;
import reactor.core.publisher.Mono;
/**
* Custom ReactiveTokenIntrospector to map realm roles into Spring GrantedAuthorities
*
*/
public class KeycloakReactiveTokenInstrospector implements ReactiveOpaqueTokenIntrospector {
private final ReactiveOpaqueTokenIntrospector delegate;
public KeycloakReactiveTokenInstrospector(ReactiveOpaqueTokenIntrospector delegate) {
this.delegate = delegate;
}
@Override
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
return delegate.introspect(token)
.map( this::mapPrincipal);
}
protected OAuth2AuthenticatedPrincipal mapPrincipal(OAuth2AuthenticatedPrincipal principal) {
return new DefaultOAuth2AuthenticatedPrincipal(
principal.getName(),
principal.getAttributes(),
extractAuthorities(principal));
}
protected Collection<GrantedAuthority> extractAuthorities(OAuth2AuthenticatedPrincipal principal) {
//
Map<String,List<String>> realm_access = principal.getAttribute("realm_access");
List<String> roles = realm_access.getOrDefault("roles", Collections.emptyList());
List<GrantedAuthority> rolesAuthorities = roles.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
Set<GrantedAuthority> allAuthorities = new HashSet<>();
allAuthorities.addAll(principal.getAuthorities());
allAuthorities.addAll(rolesAuthorities);
return allAuthorities;
}
}
@@ -0,0 +1,25 @@
/**
*
*/
package com.baeldung.springcloudgateway.rewrite;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import reactor.core.publisher.Mono;
/**
* @author Baeldung
*
*/
@SpringBootApplication
public class URLRewriteGatewayApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(URLRewriteGatewayApplication.class)
.profiles("url-rewrite")
.run(args);
}
}
@@ -0,0 +1,43 @@
package com.baeldung.springcloudgateway.rewrite.routes;
import org.springframework.beans.factory.annotation.Value;
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.context.annotation.Profile;
import org.springframework.http.server.reactive.ServerHttpRequest;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
import java.util.Random;
@Configuration
@Profile("url-rewrite")
public class DynamicRewriteRoute {
@Value("${rewrite.backend.uri}")
private String backendUri;
private static Random rnd = new Random();
@Bean
public RouteLocator dynamicZipCodeRoute(RouteLocatorBuilder builder) {
return builder.routes()
.route("dynamicRewrite", r ->
r.path("/v2/zip/**")
.filters(f -> f.filter((exchange, chain) -> {
ServerHttpRequest req = exchange.getRequest();
addOriginalRequestUrl(exchange, req.getURI());
String path = req.getURI().getRawPath();
String newPath = path.replaceAll(
"/v2/zip/(?<zipcode>.*)",
"/api/zip/${zipcode}-" + String.format("%03d", rnd.nextInt(1000)));
ServerHttpRequest request = req.mutate().path(newPath).build();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());
return chain.filter(exchange.mutate().request(request).build());
}))
.uri(backendUri))
.build();
}
}
@@ -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("url-rewrite")
.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,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
@@ -0,0 +1,8 @@
# Enable this profile to disable security
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
- org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration
- org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration
- org.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration
@@ -0,0 +1,26 @@
server:
port: 8087
spring:
cloud:
gateway:
redis:
enabled: false
routes:
- id: quotes
uri: http://localhost:8085
predicates:
- Path=/quotes/**
filters:
- TokenRelay=
security:
oauth2:
client:
provider:
keycloak:
issuer-uri: http://localhost:8083/auth/realms/baeldung
registration:
quotes-client:
provider: keycloak
client-id: quotes-client
client-secret: 0e082231-a70d-48e8-b8a5-fbfb743041b6
scope:
@@ -0,0 +1,19 @@
server:
port: 8086
spring:
security:
oauth2:
resourceserver:
opaquetoken:
introspection-uri: http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token/introspect
client-id: quotes-client
client-secret: 0e082231-a70d-48e8-b8a5-fbfb743041b6
cloud:
gateway:
redis:
enabled: false
routes:
- id: quotes
uri: http://localhost:8085
predicates:
- Path=/quotes/**
@@ -0,0 +1,11 @@
spring:
cloud:
gateway:
routes:
- id: rewrite_v1
uri: ${rewrite.backend.uri:http://example.com}
predicates:
- Path=/v1/customer/**
filters:
- RewritePath=/v1/customer/(?<segment>.*),/api/$\{segment}
@@ -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,4 @@
logging:
level:
org.springframework.cloud.gateway: DEBUG
reactor.netty.http.client: DEBUG
@@ -0,0 +1,19 @@
spring.cloud.gateway.routes[0].id=service_route
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/service/**
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/service(?<segment>/?.*), $\{segment}
spring.cloud.gateway.routes[0].filters[1]=Logging=My Custom Message, true, true
# Or, as an alternative:
#spring.cloud.gateway.routes[0].filters[1].name=Logging
#spring.cloud.gateway.routes[0].filters[1].args[baseMessage]=My Custom Message
#spring.cloud.gateway.routes[0].filters[1].args[preLogger]=true
#spring.cloud.gateway.routes[0].filters[1].args[postLogger]=true
spring.cloud.gateway.routes[0].filters[2]=ModifyResponse
spring.cloud.gateway.routes[0].filters[3]=ModifyRequest=en
spring.cloud.gateway.routes[0].filters[4]=ChainRequest=http://localhost:8082/resource/language, fr
management.endpoints.web.exposure.include=*
server.port=80
@@ -0,0 +1,7 @@
spring.cloud.gateway.routes[0].id=baeldung_route
spring.cloud.gateway.routes[0].uri=http://www.baeldung.com
spring.cloud.gateway.routes[0].predicates[0]=Path=/baeldung
management.endpoints.web.exposure.include=*
server.port=80
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,12 @@
server.port=8085
# Disable gateway & redis as we don't need them in this application
spring.cloud.gateway.enabled=false
spring.cloud.gateway.redis.enabled=false
# Resource server settings
spring.security.oauth2.resourceserver.opaquetoken.introspection-uri=http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token/introspect
spring.security.oauth2.resourceserver.opaquetoken.client-id=quotes-client
spring.security.oauth2.resourceserver.opaquetoken.client-secret=0e082231-a70d-48e8-b8a5-fbfb743041b6