From 39cee360650193af3e4e64cfdebbd778e93fefb7 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Tue, 4 Apr 2023 13:23:03 -0600 Subject: [PATCH] Use SingletonSupplier Issue gh-9991 --- .../oauth2/jwt/SupplierJwtDecoder.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/SupplierJwtDecoder.java b/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/SupplierJwtDecoder.java index bbcbdab35e..7ca791d13a 100644 --- a/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/SupplierJwtDecoder.java +++ b/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/SupplierJwtDecoder.java @@ -18,6 +18,8 @@ package org.springframework.security.oauth2.jwt; import java.util.function.Supplier; +import org.springframework.util.function.SingletonSupplier; + /** * A {@link JwtDecoder} that lazily initializes another {@link JwtDecoder} * @@ -26,12 +28,17 @@ import java.util.function.Supplier; */ public final class SupplierJwtDecoder implements JwtDecoder { - private final Supplier jwtDecoderSupplier; - - private volatile JwtDecoder delegate; + private final Supplier delegate; public SupplierJwtDecoder(Supplier jwtDecoderSupplier) { - this.jwtDecoderSupplier = jwtDecoderSupplier; + this.delegate = SingletonSupplier.of(() -> { + try { + return jwtDecoderSupplier.get(); + } + catch (Exception ex) { + throw wrapException(ex); + } + }); } /** @@ -39,19 +46,7 @@ public final class SupplierJwtDecoder implements JwtDecoder { */ @Override public Jwt decode(String token) throws JwtException { - if (this.delegate == null) { - synchronized (this.jwtDecoderSupplier) { - if (this.delegate == null) { - try { - this.delegate = this.jwtDecoderSupplier.get(); - } - catch (Exception ex) { - throw wrapException(ex); - } - } - } - } - return this.delegate.decode(token); + return this.delegate.get().decode(token); } private JwtDecoderInitializationException wrapException(Exception ex) {