Created
November 1, 2025 17:12
-
-
Save fResult/1afea27f9be1d93cc218b8ae3f457f1b to your computer and use it in GitHub Desktop.
High-Entropy VS Low-Entropy in Spring Cloud Gateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| @Profile("routes-filter") | |
| class FilterConfiguration { | |
| companion object { | |
| private val log = Loggers.getLogger(FilterConfiguration::class.java) | |
| } | |
| @Bean | |
| fun filterGateway(builder: RouteLocatorBuilder): RouteLocator = | |
| builder.routes() | |
| .route { spec -> | |
| spec.path("/") | |
| .filters { fs -> | |
| fs.setPath("/forms/post") | |
| .retry(10) | |
| .addRequestParameter("uid", UUID.randomUUID().toString()) | |
| .addResponseHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*") | |
| .filter { exchange, chain -> | |
| val uri = exchange.request.uri | |
| chain.filter(exchange) | |
| .doOnSubscribe { _ -> log.info("Before: $uri") } | |
| .doOnEach { _ -> log.info("Processing: $uri") } | |
| .doOnTerminate { log.info("After: $uri") } | |
| } | |
| } | |
| .uri("https://httpbin.org") | |
| }.build() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| @Profile("routes-filter") | |
| class FilterConfiguration { | |
| companion object { | |
| private val log = Loggers.getLogger(FilterConfiguration::class.java) | |
| } | |
| @Bean | |
| fun filterGateway(builder: RouteLocatorBuilder): RouteLocator = | |
| builder.routes() | |
| .route(::routeToFormPost) | |
| .build() | |
| private fun routeToFormPost(spec: PredicateSpec): Buildable<Route> = | |
| spec.path("/") | |
| .filters(::formPostFilters) | |
| .uri("https://httpbin.org") | |
| private fun formPostFilters(filterSpec: GatewayFilterSpec): UriSpec = | |
| filterSpec.setPath("/forms/post") | |
| .retry(10) | |
| .addRequestParameter("uid", UUID.randomUUID().toString()) | |
| .addResponseHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*") | |
| .filter(::logRequestLifecycle) | |
| private fun logRequestLifecycle(exchange: ServerWebExchange, chain: GatewayFilterChain): Mono<Void> { | |
| val uri = exchange.request.uri | |
| return chain.filter(exchange) | |
| .doOnSubscribe(logRequestInitiation(uri)) | |
| .doOnEach(logRequestProcessing(uri)) | |
| .doOnTerminate(logRequestCompletion(uri, exchange)) | |
| } | |
| private fun logRequestInitiation(uri: URI): (Subscription) -> Unit = { _ -> | |
| log.info("Before: $uri") | |
| } | |
| private fun logRequestProcessing(uri: URI): (Signal<*>) -> Unit = { _ -> | |
| log.info("Processing: $uri") | |
| } | |
| private fun logRequestCompletion(uri: URI, exchange: ServerWebExchange): () -> Unit = { | |
| log.info("After: $uri. → Status: ${exchange.response.statusCode}") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment