diff --git a/dev/com.ibm.ws.security.javaeesec/src/com/ibm/ws/security/javaeesec/BridgeBuilderImpl.java b/dev/com.ibm.ws.security.javaeesec/src/com/ibm/ws/security/javaeesec/BridgeBuilderImpl.java index 61ddf25c976d..14952b6c9581 100644 --- a/dev/com.ibm.ws.security.javaeesec/src/com/ibm/ws/security/javaeesec/BridgeBuilderImpl.java +++ b/dev/com.ibm.ws.security.javaeesec/src/com/ibm/ws/security/javaeesec/BridgeBuilderImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2020 IBM Corporation and others. + * Copyright (c) 2017, 2021 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -48,25 +48,28 @@ protected void activate(ComponentContext cc) {} @Deactivate protected void deactivate(ComponentContext cc) {} - // Synchronized since checking if there is a provider and registering one need to be done as a single atomic operation. @Override - public synchronized void buildBridgeIfNeeded(String appContext, AuthConfigFactory providerFactory) { - AuthConfigProvider authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null); - if (authConfigProvider != null) { - // A provider was registered already for this application context. - return; - } + public void buildBridgeIfNeeded(String appContext, AuthConfigFactory providerFactory) { - if (getModulePropertiesUtils().isHttpAuthenticationMechanism()) { - // Create AuthConfigProvider, AuthConfig, AuthContext, and ServerAuthModule bridge. - Map props = new ConcurrentHashMap(); - authConfigProvider = new AuthProvider(props, providerFactory); - providerFactory.registerConfigProvider(authConfigProvider, JASPIC_LAYER_HTTP_SERVLET, appContext, PROVIDER_DESCRIPTION); - } else { + if (!getModulePropertiesUtils().isHttpAuthenticationMechanism()) { if (tc.isDebugEnabled()) { Tr.debug(tc, "HttpAuthenticationMechanism bean is not identified. JSR375 BridgeProvider is not enabled."); } - } + } else { + AuthConfigProvider authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null); + if (authConfigProvider == null) { + // Synchronized since checking if there is a provider and registering one need to be done as a single atomic operation. + synchronized (this) { + authConfigProvider = providerFactory.getConfigProvider(JASPIC_LAYER_HTTP_SERVLET, appContext, (RegistrationListener) null); + if (authConfigProvider == null) { + // Create AuthConfigProvider, AuthConfig, AuthContext, and ServerAuthModule bridge. + Map props = new ConcurrentHashMap(); + authConfigProvider = new AuthProvider(props, providerFactory); + providerFactory.registerConfigProvider(authConfigProvider, JASPIC_LAYER_HTTP_SERVLET, appContext, PROVIDER_DESCRIPTION); + } + } + } + } } @Override @@ -95,4 +98,4 @@ protected ModulePropertiesUtils getModulePropertiesUtils() { return ModulePropertiesUtils.getInstance(); } -} \ No newline at end of file +} diff --git a/dev/com.ibm.ws.security.javaeesec/test/com/ibm/ws/security/javaeesec/BridgeBuilderImplTest.java b/dev/com.ibm.ws.security.javaeesec/test/com/ibm/ws/security/javaeesec/BridgeBuilderImplTest.java index 26566c34f806..8a9165392205 100644 --- a/dev/com.ibm.ws.security.javaeesec/test/com/ibm/ws/security/javaeesec/BridgeBuilderImplTest.java +++ b/dev/com.ibm.ws.security.javaeesec/test/com/ibm/ws/security/javaeesec/BridgeBuilderImplTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017 IBM Corporation and others. + * Copyright (c) 2017, 2021 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -99,7 +99,7 @@ public void tearDown() throws Exception { @Test public void testNoAuthMechDoesNotRegisterProvider() throws Exception { - withNoCachedProvider().doesNotRegisterProvider(); + withNoHAM().doesNotRegisterProvider(); isHAM = false; bridgeBuilder.buildBridgeIfNeeded(APP_CONTEXT, providerFactory); } @@ -121,15 +121,25 @@ public void testOneAuthMechRegistersProvider() throws Exception { @Test public void testWithCachedProviderDoesNotRegisterProvider() throws Exception { + isHAM = true; withCachedProvider().doesNotRegisterProvider(); - bridgeBuilder.buildBridgeIfNeeded(APP_CONTEXT, providerFactory); } private BridgeBuilderImplTest withNoCachedProvider() throws Exception { mockery.checking(new Expectations() { { - one(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null); + exactly(2).of(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null); + will(returnValue(null)); + } + }); + return this; + } + + private BridgeBuilderImplTest withNoHAM() throws Exception { + mockery.checking(new Expectations() { + { + never(providerFactory).getConfigProvider("HttpServlet", APP_CONTEXT, null); will(returnValue(null)); } });