spring-projects/spring-security · error · BeanInitializationException

Unable to create an {OAuth2AuthorizedClientManager} bean. Ex

Error message

Unable to create an {OAuth2AuthorizedClientManager} bean. Expected one bean of type {authorizedClientProvider.getClass().getName()}, but found multiple. Please consider defining only a single bean of this type, or define an {OAuth2AuthorizedClientManager} bean yourself.

What it means

Spring's OAuth2 client configuration tries to create a default OAuth2AuthorizedClientManager from a single OAuth2AuthorizedClientProvider bean found in the context. If multiple OAuth2AuthorizedClientProvider beans exist, the configuration cannot decide which to use and throws BeanInitializationException telling you to keep one provider or define your own OAuth2AuthorizedClientManager.

Source

Thrown at config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java:395

		}

		private <T extends OAuth2AuthorizedClientProvider> T getAuthorizedClientProviderByType(
				Collection<OAuth2AuthorizedClientProvider> authorizedClientProviders, Class<T> providerClass) {
			T authorizedClientProvider = null;
			for (OAuth2AuthorizedClientProvider current : authorizedClientProviders) {
				if (providerClass.isInstance(current)) {
					assertAuthorizedClientProviderIsNull(authorizedClientProvider);
					authorizedClientProvider = providerClass.cast(current);
				}
			}
			return authorizedClientProvider;
		}

		private static void assertAuthorizedClientProviderIsNull(
				OAuth2AuthorizedClientProvider authorizedClientProvider) {
			if (authorizedClientProvider != null) {
				// @formatter:off
				throw new BeanInitializationException(String.format(
						"Unable to create an %s bean. Expected one bean of type %s, but found multiple. " +
						"Please consider defining only a single bean of this type, or define an %s bean yourself.",
						OAuth2AuthorizedClientManager.class.getName(),
						authorizedClientProvider.getClass().getName(),
						OAuth2AuthorizedClientManager.class.getName()));
				// @formatter:on
			}
		}

		private <T> String[] getBeanNamesForType(Class<T> beanClass) {
			return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, beanClass, true, true);
		}

		private <T> T getBeanOfType(ResolvableType resolvableType) {
			ObjectProvider<T> objectProvider = this.beanFactory.getBeanProvider(resolvableType, true);
			return objectProvider.getIfAvailable();
		}

View on GitHub (pinned to 96852e8860)

Solutions

  1. Keep only one OAuth2AuthorizedClientProvider bean, or compose them into one (e.g. a chain-of-responsibility provider) and expose that single bean
  2. Define your own OAuth2AuthorizedClientManager @Bean so the ambiguous auto-configuration path is skipped
  3. Mark one provider @Primary or use qualifiers if multiple must coexist with a manually built manager

Example fix

// before
@Bean OAuth2AuthorizedClientProvider authCodeProvider() { ... }
@Bean OAuth2AuthorizedClientProvider clientCredsProvider() { ... }
// after
@Bean
OAuth2AuthorizedClientProvider provider() {
    return new DelegatingOAuth2AuthorizedClientProvider(authCodeProvider(), clientCredsProvider());
}
Defensive patterns

Strategy: validation

Validate before calling

String[] names = context.getBeanNamesForType(OAuth2AuthorizedClientProvider.class);
if (names.length > 1 && context.getBeanNamesForType(OAuth2AuthorizedClientManager.class).length == 0) {
    throw new IllegalStateException(
        "Multiple OAuth2AuthorizedClientProvider beans; define an OAuth2AuthorizedClientManager explicitly");
}

Try / catch

try {
    context.getBean(OAuth2AuthorizedClientManager.class);
} catch (NoSuchBeanDefinitionException | BeanCreationException e) {
    if (e.getMessage() != null && e.getMessage().contains("found multiple")) {
        // register a DelegatingOAuth2AuthorizedClientProvider and your own manager
    }
    throw e;
}

Prevention

When it happens

Trigger: Defining more than one OAuth2AuthorizedClientProvider @Bean (e.g. one for authorization_code and one for client_credentials) while relying on the auto-configured OAuth2AuthorizedClientManager; libraries contributing an additional provider bean.

Common situations: OAuth2 client apps where developers register several providers for different grant types; Spring Boot apps adding both custom and framework provider beans; upgrade scenarios where auto-configuration began consuming provider beans.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10). Data as JSON: /api/errors/b86bc17bde2f31dc. Report an issue: GitHub.