spring-projects/spring-security · error · ApplicationContextException

More than one AuthenticationUserDetailsService registered…

Error message

More than one AuthenticationUserDetailsService registered. Please use a specific Id reference.

What it means

UserDetailsServiceFactoryBean.authenticationUserDetailsService resolves an AuthenticationUserDetailsService by id. When no id is given and the application context contains more than one such bean, the factory cannot decide which to use and throws ApplicationContextException 'More than one AuthenticationUserDetailsService registered' (UserDetailsServiceFactoryBean.java:71).

Solutions

  1. Reference a specific AuthenticationUserDetailsService by id in your security configuration.
  2. Remove the duplicate AuthenticationUserDetailsService bean definitions so only one exists.
  3. Have one bean delegate to the others so exactly one bean of that type remains.

Example fix

// before (two beans, no explicit ref)
<bean id="udsA" class="MyAuthUserDetailsService"/>
<bean id="udsB" class="OtherAuthUserDetailsService"/>
// after (explicit id reference)
<http use-expressions="true" ... authentication-userDetailsService-ref="udsA"/>
<!-- or delete udsB -->
Defensive patterns

Strategy: validation

Validate before calling

int n = ctx.getBeanNamesForType(AuthenticationUserDetailsService.class).length;
if (n > 1 && !securityConfigHasExplicitRef()) { throw new IllegalStateException("Define which AuthenticationUserDetailsService (id) to use; found " + n); }

Try / catch

try { ctx.refresh(); } catch (ApplicationContextException e) { if (e.getMessage().contains("More than one AuthenticationUserDetailsService")) { log.error("Set an explicit id reference for AuthenticationUserDetailsService", e); } throw e; }

Prevention

When it happens

Trigger: Security XML config (e.g., <http> or <authentication-manager>) referencing user details without an explicit id while two or more beans of type AuthenticationUserDetailsService exist in the context, so beans.size() > 1 in authenticationUserDetailsService.

Common situations: Applications with multiple custom AuthenticationUserDetailsService beans (e.g., one per realm/tenant) combined with namespace config lacking id attributes; duplicate bean definitions from multiple imported XML files.

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/49bcadd1a1e800d1. Report an issue: GitHub.

Appendix: source

Thrown at config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java:71

		if (!StringUtils.hasText(id)) {
			return getUserDetailsService();
		}
		// Overwrite with the caching version if available
		String cachingId = id + AbstractUserDetailsServiceBeanDefinitionParser.CACHING_SUFFIX;
		if (this.beanFactory.containsBeanDefinition(cachingId)) {
			return (UserDetailsService) this.beanFactory.getBean(cachingId);
		}
		return (UserDetailsService) this.beanFactory.getBean(id);
	}

	@SuppressWarnings("unchecked")
	AuthenticationUserDetailsService authenticationUserDetailsService(String name) {
		UserDetailsService uds;
		if (!StringUtils.hasText(name)) {
			Map<String, ?> beans = getBeansOfType(AuthenticationUserDetailsService.class);
			if (!beans.isEmpty()) {
				if (beans.size() > 1) {
					throw new ApplicationContextException("More than one AuthenticationUserDetailsService registered."
							+ " Please use a specific Id reference.");
				}
				return (AuthenticationUserDetailsService) beans.values().toArray()[0];
			}
			uds = getUserDetailsService();
		}
		else {
			Object bean = this.beanFactory.getBean(name);
			if (bean instanceof AuthenticationUserDetailsService) {
				return (AuthenticationUserDetailsService) bean;
			}
			else if (bean instanceof UserDetailsService) {
				uds = cachingUserDetailsService(name);
				if (uds == null) {
					uds = (UserDetailsService) bean;
				}
			}
			else {

View on GitHub (pinned to 96852e8860)