{"record":{"id":"bd737246d510cc32","repo":"spring-projects/spring-security","slug":"more-than-one-userdetailsservice-registered-pleas","errorCode":null,"errorMessage":"More than one UserDetailsService registered. Please use a specific Id reference in <remember-me/> or <x509 /> elements.","messagePattern":"More than one UserDetailsService registered\\. Please use a specific Id reference in <remember-me/> or <x509 /> elements\\.","errorType":"exception","errorClass":"ApplicationContextException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java","lineNumber":111,"sourceCode":"\t\t}\n\t\treturn new UserDetailsByNameServiceWrapper(uds);\n\t}\n\n\t/**\n\t * Obtains a user details service for use in RememberMeServices etc. Will return a\n\t * caching version if available so should not be used for beans which need to separate\n\t * the two.\n\t */\n\tprivate UserDetailsService getUserDetailsService() {\n\t\tMap<String, ?> beans = getBeansOfType(CachingUserDetailsService.class);\n\t\tif (beans.isEmpty()) {\n\t\t\tbeans = getBeansOfType(UserDetailsService.class);\n\t\t}\n\t\tif (beans.isEmpty()) {\n\t\t\tthrow new ApplicationContextException(\"No UserDetailsService registered.\");\n\t\t}\n\t\tif (beans.size() > 1) {\n\t\t\tthrow new ApplicationContextException(\"More than one UserDetailsService registered. Please \"\n\t\t\t\t\t+ \"use a specific Id reference in <remember-me/> or <x509 /> elements.\");\n\t\t}\n\t\treturn (UserDetailsService) beans.values().toArray()[0];\n\t}\n\n\t@Override\n\tpublic void setApplicationContext(ApplicationContext beanFactory) throws BeansException {\n\t\tthis.beanFactory = beanFactory;\n\t}\n\n\tprivate Map<String, ?> getBeansOfType(Class<?> type) {\n\t\tMap<String, ?> beans = this.beanFactory.getBeansOfType(type);\n\t\t// Check ancestor bean factories if they exist and the current one has none of the\n\t\t// required type\n\t\tBeanFactory parent = this.beanFactory.getParentBeanFactory();\n\t\twhile (parent != null && beans.isEmpty()) {\n\t\t\tif (parent instanceof ListableBeanFactory) {\n\t\t\t\tbeans = ((ListableBeanFactory) parent).getBeansOfType(type);","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java#L93-L129","documentation":"When getUserDetailsService() finds multiple UserDetailsService beans and no explicit reference was given, Spring Security cannot decide which one backs remember-me/x509 token authentication, so it fails with this exception directing the user to disambiguate.","triggerScenarios":"Two or more beans of type UserDetailsService (or CachingUserDetailsService) exist and <remember-me/> or <x509/> is configured without a service-ref/user-service-ref attribute.","commonSituations":"Adding a second user store (e.g. in-memory for tests alongside JDBC) in the same context; multiple auto-configured UserDetailService sources; Spring Boot devtools restart duplicating beans.","solutions":["Specify which service to use: <remember-me service-ref=\"beanId\"/> or <x509 user-service-ref=\"beanId\"/>.","Remove or merge redundant UserDetailsService beans so only one remains.","Qualify one bean as the primary and reference it explicitly in the XML config."],"exampleFix":"// before\n<remember-me key=\"myKey\"/>\n\n// after (two uds beans exist)\n<remember-me key=\"myKey\" service-ref=\"jdbcUserService\"/>","handlingStrategy":"validation","validationCode":"int n = ctx.getBeanNamesForType(UserDetailsService.class).length;\nif (n > 1) {\n  throw new IllegalStateException(\"Multiple UserDetailServices; set service-ref explicitly\");\n}","typeGuard":null,"tryCatchPattern":"try { buildContext(); }\ncatch (ApplicationContextException e) {\n  if (e.getMessage().startsWith(\"More than one UserDetailsService\")) promptForExplicitRef();\n}","preventionTips":["Use service-ref/user-service-ref whenever more than one UDS bean may exist","Name the primary bean 'contextSource'-style canonical ids where supported","Avoid duplicate user-store beans in one context"],"tags":["spring-security","config","ambiguous-bean","userdetailsservice"],"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"}