spring-projects/spring-security · error · ApplicationContextException

More than one BaseLdapPathContextSource instance found. Plea

Error message

More than one BaseLdapPathContextSource instance found. Please specify a specific server id using the 'server-ref' attribute when configuring your <ldap-authentication-provider> or <ldap-user-service>.

What it means

When multiple BaseLdapPathContextSource beans exist and no bean is named 'contextSource', the post-processor cannot choose a default for elements like <ldap-authentication-provider> or <ldap-user-service> that omitted server-ref, so it throws instead of guessing.

Source

Thrown at config/src/main/java/org/springframework/security/config/ldap/ContextSourceSettingPostProcessor.java:62

	 * needs to be set.
	 */
	private boolean defaultNameRequired;

	ContextSourceSettingPostProcessor() {
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
		Class<?> contextSourceClass = getContextSourceClass();
		String[] sources = bf.getBeanNamesForType(contextSourceClass, false, false);
		if (sources.length == 0) {
			throw new ApplicationContextException("No BaseLdapPathContextSource instances found. Have you "
					+ "added an <" + Elements.LDAP_SERVER + " /> element to your application context? If you have "
					+ "declared an explicit bean, do not use lazy-init");
		}
		if (!bf.containsBean(BeanIds.CONTEXT_SOURCE) && this.defaultNameRequired) {
			if (sources.length > 1) {
				throw new ApplicationContextException("More than one BaseLdapPathContextSource instance found. "
						+ "Please specify a specific server id using the 'server-ref' attribute when configuring your <"
						+ Elements.LDAP_PROVIDER + "> " + "or <" + Elements.LDAP_USER_SERVICE + ">.");
			}
			bf.registerAlias(sources[0], BeanIds.CONTEXT_SOURCE);
		}
	}

	private Class<?> getContextSourceClass() throws LinkageError {
		try {
			return ClassUtils.forName(REQUIRED_CONTEXT_SOURCE_CLASS_NAME, ClassUtils.getDefaultClassLoader());
		}
		catch (ClassNotFoundException ex) {
			throw new ApplicationContextException("Couldn't locate: " + REQUIRED_CONTEXT_SOURCE_CLASS_NAME + ". "
					+ " If you are using LDAP with Spring Security, please ensure that you include the spring-ldap "
					+ "jar file in your application", ex);
		}
	}

View on GitHub (pinned to 96852e8860)

Solutions

  1. Add server-ref="serverId" to <ldap-authentication-provider/> and <ldap-user-service/> elements.
  2. Name the primary context source bean 'contextSource' (bf.containsBean(BeanIds.CONTEXT_SOURCE) bypasses the ambiguity check).
  3. Remove duplicate <ldap-server/> definitions if only one server is needed.

Example fix

// before
<ldap-server id="srv1" url="ldap://a"/>
<ldap-server id="srv2" url="ldap://b"/>
<ldap-authentication-provider user-search-filter="(uid={0})"/>

// after
<ldap-authentication-provider user-search-filter="(uid={0})" server-ref="srv1"/>
Defensive patterns

Strategy: validation

Validate before calling

String[] names = bf.getBeanNamesForType(BaseLdapPathContextSource.class, false, false);
if (names.length > 1 && !bf.containsBean("contextSource")) {
  throw new IllegalStateException("Set server-ref on each LDAP consumer");
}

Try / catch

try { ctx.refresh(); }
catch (ApplicationContextException e) {
  if (e.getMessage().contains("More than one BaseLdapPathContextSource")) log.error("Add server-ref attribute");
}

Prevention

When it happens

Trigger: More than one BaseLdapPathContextSource bean in the context (e.g. two <ldap-server/> elements or multiple explicit context sources) while <ldap-authentication-provider/> or <ldap-user-service/> omit the server-ref attribute and defaultNameRequired is true.

Common situations: Connecting to two LDAP servers (e.g. one for users, one for groups) without wiring each provider/service to its source; copy-pasting ldap-server definitions.

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/6098af067a08dc0b. Report an issue: GitHub.