diff --git a/sts-token-auth/pom.xml b/sts-token-auth/pom.xml index 540ca86a..82fe3c6d 100644 --- a/sts-token-auth/pom.xml +++ b/sts-token-auth/pom.xml @@ -48,5 +48,10 @@ mockito-core test + + org.springframework.retry + spring-retry + 2.0.5 + diff --git a/sts-token-auth/src/main/java/de/adorsys/sts/tokenauth/AuthServer.java b/sts-token-auth/src/main/java/de/adorsys/sts/tokenauth/AuthServer.java index d1dab3b0..cd79f43a 100644 --- a/sts-token-auth/src/main/java/de/adorsys/sts/tokenauth/AuthServer.java +++ b/sts-token-auth/src/main/java/de/adorsys/sts/tokenauth/AuthServer.java @@ -9,6 +9,8 @@ import lombok.Setter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Retryable; import java.net.URL; import java.security.Key; @@ -68,6 +70,8 @@ private void updateJwkCache() throws JsonWebKeyRetrievalException { log.debug("Thread leaving updateJwkCache: " + Thread.currentThread().getId()); } + @Retryable(maxAttempts = 2, backoff = @Backoff(delay = 1000, multiplier = 2), retryFor = + {JsonWebKeyNotFoundException.class}, noRetryFor = {JsonWebKeyRetrievalException.class}) public Key getJWK(String keyID) throws JsonWebKeyRetrievalException { log.debug("Thread entering getJWK: {}", Thread.currentThread().getId()); @@ -84,7 +88,9 @@ public Key getJWK(String keyID) throws JsonWebKeyRetrievalException { JWK jwk = jwkCache.get(keyID); if (jwk == null) { log.error("Key with ID {} not found in cache", keyID); - throw new JsonWebKeyRetrievalException("Key with ID " + keyID + " not found in cache"); + //Update cache and try again + updateJwkCache(); + throw new JsonWebKeyNotFoundException("Key with ID " + keyID + " not found in cache"); } log.debug("JWK for key ID {} found in cache", keyID); @@ -119,4 +125,10 @@ public JsonWebKeyRetrievalException(String message) { super(message); } } + + protected static class JsonWebKeyNotFoundException extends RuntimeException { + public JsonWebKeyNotFoundException(String message) { + super(message); + } + } }