spring-projects/spring-security · error · ApplicationContextException
Couldn't locate: org.springframework.ldap.core.support.BaseL
Error message
Couldn't locate: org.springframework.ldap.core.support.BaseLdapPathContextSource. If you are using LDAP with Spring Security, please ensure that you include the spring-ldap jar file in your application
What it means
getContextSourceClass() loads org.springframework.ldap.core.support.BaseLdapPathContextSource reflectively. If spring-ldap is not on the classpath the class cannot be found, and an ApplicationContextException (wrapping ClassNotFoundException) is thrown since LDAP support cannot function without it.
Source
Thrown at config/src/main/java/org/springframework/security/config/ldap/ContextSourceSettingPostProcessor.java:75
+ "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);
}
}
public void setDefaultNameRequired(boolean defaultNameRequired) {
this.defaultNameRequired = defaultNameRequired;
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}
}
View on GitHub (pinned to 96852e8860)
Solutions
- Add spring-ldap-core to dependencies (e.g. org.springframework.ldap:spring-ldap-core).
- Verify the dependency isn't marked with a scope/feature that excludes it from the runtime classpath (e.g. 'provided' in a war).
- If LDAP is not used, remove the LDAP configuration elements so the post-processor never runs.
Example fix
// before <!-- pom.xml: spring-security-config only --> // after <dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> </dependency>
Defensive patterns
Strategy: validation
Validate before calling
try {
ClassUtils.forName("org.springframework.ldap.core.support.BaseLdapPathContextSource",
ClassUtils.getDefaultClassLoader());
} catch (ClassNotFoundException ex) {
throw new IllegalStateException("Add spring-ldap-core to the classpath", ex);
} Try / catch
try { enableLdap(); }
catch (ApplicationContextException e) {
if (e.getMessage().contains("Couldn't locate")) addSpringLdapDependency();
} Prevention
- Add spring-ldap-core whenever any LDAP XML element is used
- Check dependency tree (mvn dependency:tree) for spring-ldap
- Remove unused LDAP config elements instead of leaving them half-configured
When it happens
Trigger: Any LDAP configuration (<ldap-server/>, <ldap-authentication-provider/>, <ldap-user-service/>) present while the spring-ldap-core jar is absent from the application classpath.
Common situations: Using spring-security-config LDAP XML namespace without adding the spring-ldap dependency; Maven/Gradle dependency scope wrong (provided/test) so it's missing at runtime; upgrading Spring Security which requires newer spring-ldap.
Related errors
- Embedded LDAP server is not provided
- Cannot find JSON Converter on the classpath. Add one followi
- managerPassword is required if managerDn is supplied
- Embedded LDAP server is not provided
- oidc_provider_not_configured
AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10).
Data as JSON: /api/errors/3e79146083efea1b.
Report an issue: GitHub.