@@ -1254,33 +1254,45 @@ Collection<Object> createComponents(
12541254 }
12551255
12561256 private CustomApiKeyAuthenticator createCustomApiKeyAuthenticator (SecurityExtension .SecurityComponents extensionComponents ) {
1257- final SetOnce < CustomApiKeyAuthenticator > customApiKeyAuthenticatorSetOnce = new SetOnce <>();
1258- for (var extension : securityExtensions ) {
1257+ final Map < String , CustomApiKeyAuthenticator > customApiKeyAuthenticatorByExtension = new HashMap <>();
1258+ for (final SecurityExtension extension : securityExtensions ) {
12591259 final CustomApiKeyAuthenticator customApiKeyAuthenticator = extension .getCustomApiKeyAuthenticator (extensionComponents );
12601260 if (customApiKeyAuthenticator != null ) {
12611261 if (false == isInternalExtension (extension )) {
12621262 throw new IllegalStateException (
12631263 "The ["
1264- + extension .getClass (). getName ()
1264+ + extension .extensionName ()
12651265 + "] extension tried to install a custom CustomApiKeyAuthenticator. "
12661266 + "This functionality is not available to external extensions."
12671267 );
12681268 }
1269- boolean success = customApiKeyAuthenticatorSetOnce .trySet (customApiKeyAuthenticator );
1270- if (false == success ) {
1271- throw new IllegalStateException (
1272- "The ["
1273- + extension .getClass ().getName ()
1274- + "] extension tried to install a custom CustomApiKeyAuthenticator, but one has already been installed."
1275- );
1276- }
1277- logger .debug ("CustomApiKeyAuthenticator provided by extension [{}]" , extension .extensionName ());
1269+ customApiKeyAuthenticatorByExtension .put (extension .extensionName (), customApiKeyAuthenticator );
12781270 }
12791271 }
1280- if (customApiKeyAuthenticatorSetOnce .get () == null ) {
1281- customApiKeyAuthenticatorSetOnce .set (new CustomApiKeyAuthenticator .Noop ());
1272+
1273+ if (customApiKeyAuthenticatorByExtension .isEmpty ()) {
1274+ logger .debug (
1275+ "No custom implementation for [{}]. Falling-back to noop implementation." ,
1276+ CustomApiKeyAuthenticator .class .getCanonicalName ()
1277+ );
1278+ return new CustomApiKeyAuthenticator .Noop ();
1279+
1280+ } else if (customApiKeyAuthenticatorByExtension .size () > 1 ) {
1281+ throw new IllegalStateException (
1282+ "Multiple extensions tried to install a custom CustomApiKeyAuthenticator: " + customApiKeyAuthenticatorByExtension .keySet ()
1283+ );
1284+
1285+ } else {
1286+ final var authenticatorByExtensionEntry = customApiKeyAuthenticatorByExtension .entrySet ().iterator ().next ();
1287+ final CustomApiKeyAuthenticator customApiKeyAuthenticator = authenticatorByExtensionEntry .getValue ();
1288+ final String extensionName = authenticatorByExtensionEntry .getKey ();
1289+ logger .debug (
1290+ "CustomApiKeyAuthenticator implementation [{}] provided by extension [{}]" ,
1291+ customApiKeyAuthenticator .getClass ().getCanonicalName (),
1292+ extensionName
1293+ );
1294+ return customApiKeyAuthenticator ;
12821295 }
1283- return customApiKeyAuthenticatorSetOnce .get ();
12841296 }
12851297
12861298 private ServiceAccountService createServiceAccountService (
0 commit comments