From dba19c73c7ccdc0401a3a4ee401690a07fbcab08 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 25 Mar 2026 11:05:28 +0100 Subject: [PATCH] Polishing contribution See gh-49744 Signed-off-by: Brian Clozel --- .../endpoint/reactive/SecurityService.java | 33 ++++++++----------- .../reactive/SecurityServiceTests.java | 4 +++ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java b/module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java index 462741cb408..a26ea579c18 100644 --- a/module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java +++ b/module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityService.java @@ -22,7 +22,6 @@ import java.util.Map; import io.netty.handler.ssl.SslProvider; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; -import org.jspecify.annotations.Nullable; import reactor.core.publisher.Mono; import reactor.netty.http.Http11SslContextSpec; import reactor.netty.http.client.HttpClient; @@ -55,7 +54,7 @@ class SecurityService { private final String cloudControllerUrl; - private volatile @Nullable String uaaUrl; + private final Mono uaaUrl; SecurityService(WebClient.Builder webClientBuilder, String cloudControllerUrl, boolean skipSslValidation) { Assert.notNull(webClientBuilder, "'webClientBuilder' must not be null"); @@ -65,6 +64,18 @@ class SecurityService { } this.webClient = webClientBuilder.build(); this.cloudControllerUrl = cloudControllerUrl; + this.uaaUrl = this.webClient.get() + .uri(this.cloudControllerUrl + "/info") + .retrieve() + .bodyToMono(Map.class) + .map((response) -> { + String tokenEndpoint = (String) response.get("token_endpoint"); + Assert.state(tokenEndpoint != null, "No 'token_endpoint' found in response"); + return tokenEndpoint; + }) + .cacheInvalidateIf((token) -> false) + .onErrorMap((ex) -> new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE, + "Unable to fetch token keys from UAA.")); } protected ReactorClientHttpConnector buildTrustAllSslConnector() { @@ -152,23 +163,7 @@ class SecurityService { * @return the UAA url Mono */ Mono getUaaUrl() { - String uaaUrl = this.uaaUrl; - if (uaaUrl != null) { - return Mono.just(uaaUrl); - } - return this.webClient.get() - .uri(this.cloudControllerUrl + "/info") - .retrieve() - .bodyToMono(Map.class) - .map((response) -> { - String tokenEndpoint = (String) response.get("token_endpoint"); - Assert.state(tokenEndpoint != null, "No 'token_endpoint' found in response"); - this.uaaUrl = tokenEndpoint; - return tokenEndpoint; - }) - .cache() - .onErrorMap((ex) -> new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE, - "Unable to fetch token keys from UAA.")); + return this.uaaUrl; } } diff --git a/module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java b/module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java index 35675aba4e9..11cf987ba62 100644 --- a/module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java +++ b/module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/reactive/SecurityServiceTests.java @@ -213,6 +213,10 @@ class SecurityServiceTests { .consumeNextWith((uaaUrl) -> assertThat(uaaUrl).isEqualTo(UAA_URL)) .expectComplete() .verify(); + prepareResponse((response) -> { + response.setBody("{\"token_endpoint\":\"" + UAA_URL + "\"}"); + response.setHeader("Content-Type", "application/json"); + }); StepVerifier.create(this.securityService.getUaaUrl()) .consumeNextWith((uaaUrl) -> assertThat(uaaUrl).isEqualTo(UAA_URL)) .expectComplete()