|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2019 the original author or authors.
|
|
|
+ * Copyright 2002-2023 the original author or authors.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -43,23 +43,33 @@ class ReactiveRemoteJWKSource implements ReactiveJWKSource {
|
|
|
*/
|
|
|
private final AtomicReference<Mono<JWKSet>> cachedJWKSet = new AtomicReference<>(Mono.empty());
|
|
|
|
|
|
+ /**
|
|
|
+ * cached url for jwk set.
|
|
|
+ */
|
|
|
+ private final AtomicReference<String> cachedJwkSetUrl = new AtomicReference<>();
|
|
|
+
|
|
|
private WebClient webClient = WebClient.create();
|
|
|
|
|
|
- private final String jwkSetURL;
|
|
|
+ private Mono<String> jwkSetURLProvider;
|
|
|
|
|
|
ReactiveRemoteJWKSource(String jwkSetURL) {
|
|
|
Assert.hasText(jwkSetURL, "jwkSetURL cannot be empty");
|
|
|
- this.jwkSetURL = jwkSetURL;
|
|
|
+ this.cachedJwkSetUrl.set(jwkSetURL);
|
|
|
+ }
|
|
|
+
|
|
|
+ ReactiveRemoteJWKSource(Mono<String> jwkSetURLProvider) {
|
|
|
+ Assert.notNull(jwkSetURLProvider, "jwkSetURLProvider cannot be null");
|
|
|
+ this.jwkSetURLProvider = jwkSetURLProvider;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Mono<List<JWK>> get(JWKSelector jwkSelector) {
|
|
|
// @formatter:off
|
|
|
return this.cachedJWKSet.get()
|
|
|
- .switchIfEmpty(Mono.defer(() -> getJWKSet()))
|
|
|
+ .switchIfEmpty(Mono.defer(this::getJWKSet))
|
|
|
.flatMap((jwkSet) -> get(jwkSelector, jwkSet))
|
|
|
.switchIfEmpty(Mono.defer(() -> getJWKSet()
|
|
|
- .map((jwkSet) -> jwkSelector.select(jwkSet)))
|
|
|
+ .map(jwkSelector::select))
|
|
|
);
|
|
|
// @formatter:on
|
|
|
}
|
|
@@ -95,13 +105,18 @@ class ReactiveRemoteJWKSource implements ReactiveJWKSource {
|
|
|
*/
|
|
|
private Mono<JWKSet> getJWKSet() {
|
|
|
// @formatter:off
|
|
|
- return this.webClient.get()
|
|
|
- .uri(this.jwkSetURL)
|
|
|
- .retrieve()
|
|
|
- .bodyToMono(String.class)
|
|
|
+ return Mono.justOrEmpty(this.cachedJwkSetUrl.get())
|
|
|
+ .switchIfEmpty(Mono.defer(() -> this.jwkSetURLProvider
|
|
|
+ .doOnNext(this.cachedJwkSetUrl::set))
|
|
|
+ )
|
|
|
+ .flatMap((jwkSetURL) -> this.webClient.get()
|
|
|
+ .uri(jwkSetURL)
|
|
|
+ .retrieve()
|
|
|
+ .bodyToMono(String.class)
|
|
|
+ )
|
|
|
.map(this::parse)
|
|
|
.doOnNext((jwkSet) -> this.cachedJWKSet
|
|
|
- .set(Mono.just(jwkSet))
|
|
|
+ .set(Mono.just(jwkSet))
|
|
|
)
|
|
|
.cache();
|
|
|
// @formatter:on
|