{"record":{"id":"da75ef984e78bcb8","repo":"spring-projects/spring-security","slug":"bean-name-must-be-a-userdetailsservice-or-an-a","errorCode":null,"errorMessage":"Bean '{name}' must be a UserDetailsService or an AuthenticationUserDetailsService","messagePattern":"Bean '(.+?)' must be a UserDetailsService or an AuthenticationUserDetailsService","errorType":"exception","errorClass":"ApplicationContextException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java","lineNumber":90,"sourceCode":"\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 {\n\t\t\t\tthrow new ApplicationContextException(\n\t\t\t\t\t\t\"Bean '\" + name + \"' must be a UserDetailsService or an\" + \" AuthenticationUserDetailsService\");\n\t\t\t}\n\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.\");","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java#L72-L108","documentation":"UserDetailsServiceFactoryBean.authenticationUserDetailsService() resolves a bean reference into a UserDetailsService wrapper for remember-me/x509 support. After the bean is fetched, it must be an AuthenticationUserDetailsService or a UserDetailsService (or CachingUserDetailsService factory product). If the referenced bean is neither, an ApplicationContextException is thrown because no UserDetails lookup strategy can be derived from it.","triggerScenarios":"An <http> remember-me service-ref or x509 service-ref points at a bean whose type is neither UserDetailsService nor AuthenticationUserDetailsService — e.g. referencing a plain DAO, a service class, or a bean typed Object/generic.","commonSituations":"Typo'd bean id that resolves to the wrong bean; refactoring moved UserDetails logic into a wrapper class no longer implementing UserDetailsService; using an XML ref to a @Component that doesn't implement the interface.","solutions":["Make the referenced bean implement org.springframework.security.core.userdetails.UserDetailsService (loadUserByUsername) or AuthenticationUserDetailsService.","Point service-ref at an existing UserDetailsService bean instead of the wrong bean.","If the bean is a CachingUserDetailsService produced by a factory, ensure the factory-bean type is correctly declared so it is recognized as a UserDetailsService."],"exampleFix":"// before\n<bean id=\"userService\" class=\"com.app.UserService\"/>\n<remember-me service-ref=\"userService\"/>\n\n// after\n<bean id=\"userService\" class=\"com.app.UserService\"/>\n<bean id=\"uds\" class=\"com.app.CustomUserDetailsService\"/>\n<remember-me service-ref=\"uds\"/>","handlingStrategy":"type-guard","validationCode":"Object bean = ctx.getBean(refName);\nif (!(bean instanceof UserDetailsService) && !(bean instanceof AuthenticationUserDetailsService)) {\n    throw new IllegalArgumentException(refName + \" must be a UserDetailsService\");\n}","typeGuard":"function isUds(Object b) {\n  return b instanceof org.springframework.security.core.userdetails.UserDetailsService\n      || b instanceof org.springframework.security.core.userdetails.AuthenticationUserDetailsService;\n}","tryCatchPattern":"try { svc = factory.authenticationUserDetailsService(refName); }\ncatch (ApplicationContextException e) { log.error(\"Bean {} is not a UserDetailsService\", refName, e); }","preventionTips":["Only point service-ref at beans implementing UserDetailsService","Add an integration test that boots the full security config","Check bean types in context startup validation"],"tags":["spring-security","config","bean-type-mismatch","userdetailsservice"],"backgroundTag":"type-mismatch","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}