{"record":{"id":"49bcadd1a1e800d1","repo":"spring-projects/spring-security","slug":"more-than-one-authenticationuserdetailsservice-reg","errorCode":null,"errorMessage":"More than one AuthenticationUserDetailsService registered. Please use a specific Id reference.","messagePattern":"More than one AuthenticationUserDetailsService registered\\. Please use a specific Id reference\\.","errorType":"exception","errorClass":"ApplicationContextException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java","lineNumber":71,"sourceCode":"\t\tif (!StringUtils.hasText(id)) {\n\t\t\treturn getUserDetailsService();\n\t\t}\n\t\t// Overwrite with the caching version if available\n\t\tString cachingId = id + AbstractUserDetailsServiceBeanDefinitionParser.CACHING_SUFFIX;\n\t\tif (this.beanFactory.containsBeanDefinition(cachingId)) {\n\t\t\treturn (UserDetailsService) this.beanFactory.getBean(cachingId);\n\t\t}\n\t\treturn (UserDetailsService) this.beanFactory.getBean(id);\n\t}\n\n\t@SuppressWarnings(\"unchecked\")\n\tAuthenticationUserDetailsService authenticationUserDetailsService(String name) {\n\t\tUserDetailsService uds;\n\t\tif (!StringUtils.hasText(name)) {\n\t\t\tMap<String, ?> beans = getBeansOfType(AuthenticationUserDetailsService.class);\n\t\t\tif (!beans.isEmpty()) {\n\t\t\t\tif (beans.size() > 1) {\n\t\t\t\t\tthrow new ApplicationContextException(\"More than one AuthenticationUserDetailsService registered.\"\n\t\t\t\t\t\t\t+ \" Please use a specific Id reference.\");\n\t\t\t\t}\n\t\t\t\treturn (AuthenticationUserDetailsService) beans.values().toArray()[0];\n\t\t\t}\n\t\t\tuds = getUserDetailsService();\n\t\t}\n\t\telse {\n\t\t\tObject bean = this.beanFactory.getBean(name);\n\t\t\tif (bean instanceof AuthenticationUserDetailsService) {\n\t\t\t\treturn (AuthenticationUserDetailsService) bean;\n\t\t\t}\n\t\t\telse if (bean instanceof UserDetailsService) {\n\t\t\t\tuds = cachingUserDetailsService(name);\n\t\t\t\tif (uds == null) {\n\t\t\t\t\tuds = (UserDetailsService) bean;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java#L53-L89","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reference a specific AuthenticationUserDetailsService by id in your security configuration.","Remove the duplicate AuthenticationUserDetailsService bean definitions so only one exists.","Have one bean delegate to the others so exactly one bean of that type remains."],"exampleFix":"// before (two beans, no explicit ref)\n<bean id=\"udsA\" class=\"MyAuthUserDetailsService\"/>\n<bean id=\"udsB\" class=\"OtherAuthUserDetailsService\"/>\n// after (explicit id reference)\n<http use-expressions=\"true\" ... authentication-userDetailsService-ref=\"udsA\"/>\n<!-- or delete udsB -->","handlingStrategy":"validation","validationCode":"int n = ctx.getBeanNamesForType(AuthenticationUserDetailsService.class).length;\nif (n > 1 && !securityConfigHasExplicitRef()) { throw new IllegalStateException(\"Define which AuthenticationUserDetailsService (id) to use; found \" + n); }","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Reference user details services by explicit id in security namespace config","Keep at most one AuthenticationUserDetailsService bean, using delegation for multi-realm cases","Review imported XML fragments for duplicate bean definitions of this type"],"tags":["spring-security","bean-lookup","ambiguity","user-details-service"],"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-23T08:17:48.524Z"}