{"record":{"id":"6098af067a08dc0b","repo":"spring-projects/spring-security","slug":"more-than-one-baseldappathcontextsource-instance-f","errorCode":null,"errorMessage":"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>.","messagePattern":"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>\\.","errorType":"exception","errorClass":"ApplicationContextException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/ldap/ContextSourceSettingPostProcessor.java","lineNumber":62,"sourceCode":"\t * needs to be set.\n\t */\n\tprivate boolean defaultNameRequired;\n\n\tContextSourceSettingPostProcessor() {\n\t}\n\n\t@Override\n\tpublic void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {\n\t\tClass<?> contextSourceClass = getContextSourceClass();\n\t\tString[] sources = bf.getBeanNamesForType(contextSourceClass, false, false);\n\t\tif (sources.length == 0) {\n\t\t\tthrow new ApplicationContextException(\"No BaseLdapPathContextSource instances found. Have you \"\n\t\t\t\t\t+ \"added an <\" + Elements.LDAP_SERVER + \" /> element to your application context? If you have \"\n\t\t\t\t\t+ \"declared an explicit bean, do not use lazy-init\");\n\t\t}\n\t\tif (!bf.containsBean(BeanIds.CONTEXT_SOURCE) && this.defaultNameRequired) {\n\t\t\tif (sources.length > 1) {\n\t\t\t\tthrow new ApplicationContextException(\"More than one BaseLdapPathContextSource instance found. \"\n\t\t\t\t\t\t+ \"Please specify a specific server id using the 'server-ref' attribute when configuring your <\"\n\t\t\t\t\t\t+ Elements.LDAP_PROVIDER + \"> \" + \"or <\" + Elements.LDAP_USER_SERVICE + \">.\");\n\t\t\t}\n\t\t\tbf.registerAlias(sources[0], BeanIds.CONTEXT_SOURCE);\n\t\t}\n\t}\n\n\tprivate Class<?> getContextSourceClass() throws LinkageError {\n\t\ttry {\n\t\t\treturn ClassUtils.forName(REQUIRED_CONTEXT_SOURCE_CLASS_NAME, ClassUtils.getDefaultClassLoader());\n\t\t}\n\t\tcatch (ClassNotFoundException ex) {\n\t\t\tthrow new ApplicationContextException(\"Couldn't locate: \" + REQUIRED_CONTEXT_SOURCE_CLASS_NAME + \". \"\n\t\t\t\t\t+ \" If you are using LDAP with Spring Security, please ensure that you include the spring-ldap \"\n\t\t\t\t\t+ \"jar file in your application\", ex);\n\t\t}\n\t}\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/ldap/ContextSourceSettingPostProcessor.java#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add server-ref=\"serverId\" to <ldap-authentication-provider/> and <ldap-user-service/> elements.","Name the primary context source bean 'contextSource' (bf.containsBean(BeanIds.CONTEXT_SOURCE) bypasses the ambiguity check).","Remove duplicate <ldap-server/> definitions if only one server is needed."],"exampleFix":"// before\n<ldap-server id=\"srv1\" url=\"ldap://a\"/>\n<ldap-server id=\"srv2\" url=\"ldap://b\"/>\n<ldap-authentication-provider user-search-filter=\"(uid={0})\"/>\n\n// after\n<ldap-authentication-provider user-search-filter=\"(uid={0})\" server-ref=\"srv1\"/>","handlingStrategy":"validation","validationCode":"String[] names = bf.getBeanNamesForType(BaseLdapPathContextSource.class, false, false);\nif (names.length > 1 && !bf.containsBean(\"contextSource\")) {\n  throw new IllegalStateException(\"Set server-ref on each LDAP consumer\");\n}","typeGuard":null,"tryCatchPattern":"try { ctx.refresh(); }\ncatch (ApplicationContextException e) {\n  if (e.getMessage().contains(\"More than one BaseLdapPathContextSource\")) log.error(\"Add server-ref attribute\");\n}","preventionTips":["With multiple LDAP servers, always specify server-ref","Keep a single context source unless explicitly needed","Name the default source bean 'contextSource'"],"tags":["spring-security","ldap","ambiguous-bean","config"],"backgroundTag":"conflicting-config-options","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}