{"record":{"id":"551a180d770141ad","repo":"paascloud/paascloud-master","slug":"usernamenotfoundexception-username","errorCode":null,"errorMessage":"UsernameNotFoundException(username)","messagePattern":"UsernameNotFoundException\\(username\\)","errorType":"http","errorClass":"UsernameNotFoundException","httpStatus":401,"severity":"error","filePath":"paascloud-common/paascloud-security-core/src/main/java/com/paascloud/security/core/authentication/DefaultUserDetailsServiceImpl.java","lineNumber":29,"sourceCode":" *\n * @author paascloud.net @gmail.com\n */\n@Slf4j\npublic class DefaultUserDetailsServiceImpl implements UserDetailsService {\n\n\t/**\n\t * Load user by username user details.\n\t *\n\t * @param username the username\n\t *\n\t * @return the user details\n\t *\n\t * @throws UsernameNotFoundException the username not found exception\n\t */\n\t@Override\n\tpublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {\n\t\tlog.warn(\"请配置 UserDetailsService 接口的实现.\");\n\t\tthrow new UsernameNotFoundException(username);\n\t}\n\n}\n","sourceCodeStart":11,"sourceCodeEnd":33,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-common/paascloud-security-core/src/main/java/com/paascloud/security/core/authentication/DefaultUserDetailsServiceImpl.java#L11-L33","documentation":"DefaultUserDetailsServiceImpl is a placeholder implementation of Spring Security's UserDetailsService that always throws UsernameNotFoundException(username). It lets the security core compile without a real user store; hitting it means no application-specific UserDetailsService bean is registered, so username/password (and SMS) login can never authenticate.","triggerScenarios":"Any form/SMS login authentication flow invoking loadUserByUsername while the application has not defined its own UserDetailsService implementation, or the implementation is not registered as a bean so the default stub is injected.","commonSituations":"Fresh integration of paascloud-security modules; the real UserDetailsService exists but lacks @Service or is outside component-scan; multiple UserDetailsService beans cause the default to be injected unexpectedly; using the wrong module (core demo) in production.","solutions":["Implement UserDetailsService.loadUserByUsername to load users (including encoded password and authorities).","Annotate it @Service (or expose via @Bean) so it replaces DefaultUserDetailsServiceImpl.","Check component-scan base packages include your implementation's package.","Verify DaoAuthenticationProvider is wired to your bean, not the default.","Never return null — throw UsernameNotFoundException for unknown users."],"exampleFix":"// before\n// missing implementation, default stub throws\n// after\n@Service\npublic class MyUserDetailsService implements UserDetailsService {\n    @Override\n    public UserDetails loadUserByUsername(String username) {\n        return userRepository.findByUsername(username)\n            .map(SecurityUser::new)\n            .orElseThrow(() -> new UsernameNotFoundException(username));\n    }\n}","handlingStrategy":"try-catch","validationCode":"if (userDetailsService instanceof DefaultUserDetailsServiceImpl) {\n    throw new IllegalStateException(\"Configure a real UserDetailsService before enabling login\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    UserDetails u = userDetailsService.loadUserByUsername(username);\n} catch (UsernameNotFoundException e) {\n    log.error(\"UserDetailsService not implemented or user missing: {}\", e.getMessage());\n    throw new BadCredentialsException(\"Invalid username or password\");\n}","preventionTips":["Provide an application UserDetailsService bean at project bootstrap.","Add a startup check that rejects the default stub in production profiles.","Test login flow end-to-end before release."],"tags":["java","spring-security","stub-implementation","userdetails"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}