{"record":{"id":"35ac0e67c505bde8","repo":"alibaba/nacos","slug":"user-s-not-found","errorCode":null,"errorMessage":"User %s not found","messagePattern":"User (.+?) not found","errorType":"exception","errorClass":"UsernameNotFoundException","httpStatus":null,"severity":"warning","filePath":"plugin-default-impl/nacos-default-auth-plugin/src/main/java/com/alibaba/nacos/plugin/auth/impl/users/NacosUserServiceDirectImpl.java","lineNumber":56,"sourceCode":"    private final UserPersistService userPersistService;\n    \n    private final NacosAuthPluginConfigProvider configProvider;\n    \n    public NacosUserServiceDirectImpl(NacosAuthPluginConfigProvider configProvider,\n        UserPersistService userPersistService) {\n        super();\n        this.userPersistService = userPersistService;\n        this.configProvider = configProvider;\n    }\n    \n    @Override\n    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {\n        User user = getCachedUserMap().get(username);\n        if (!configProvider.getConfig().isCachingEnabled()) {\n            user = getUser(username);\n        }\n        if (user == null) {\n            throw new UsernameNotFoundException(String.format(\"User %s not found\", username));\n        }\n        return new NacosUserDetails(user);\n    }\n    \n    @Override\n    public void updateUserPassword(String username, String password) {\n        userPersistService.updateUserPassword(username, PasswordEncoderUtil.encode(password));\n    }\n    \n    @Override\n    public Page<User> getUsers(int pageNo, int pageSize, String username) {\n        return userPersistService.getUsers(pageNo, pageSize, username);\n    }\n    \n    @Override\n    public User getUser(String username) {\n        return userPersistService.findUserByUsername(username);\n    }","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/plugin-default-impl/nacos-default-auth-plugin/src/main/java/com/alibaba/nacos/plugin/auth/impl/users/NacosUserServiceDirectImpl.java#L38-L74","documentation":"Implements Spring Security's UserDetailsService.loadUserByUsername for the direct (DB-backed) impl. When the user is not found in the cache — or, with caching disabled, not found via the persistence layer — it throws UsernameNotFoundException. This is the expected signal for 'unknown user' during login, which Spring Security maps to an authentication failure (typically 401).","triggerScenarios":"A login attempt with a username that does not exist in the database; caching enabled but the user cache has not yet been populated by reload() and the user genuinely does not exist; the user was deleted between cache refreshes.","commonSituations":"Typo in the username; a deleted user still referenced by a client; a fresh node whose user cache has not loaded yet.","solutions":["Confirm the username exists in the users table (or via the /user/list API).","If caching is on and the node just started, wait for the reload() sweep (every 15s) or force a cache refresh.","Map UsernameNotFoundException to a clean 401/invalid-credentials response at the controller boundary.","Check that you are querying the right namespace/environment."],"exampleFix":"// before\nUserDetails ud = userService.loadUserByUsername(username); // throws if unknown\n\n// after\ntry {\n    UserDetails ud = userService.loadUserByUsername(username);\n} catch (UsernameNotFoundException e) {\n    // expected: unknown user -> standard auth failure\n    throw new BadCredentialsException(\"invalid credentials\");\n}","handlingStrategy":"try-catch","validationCode":"// Optional: confirm the user exists before triggering Spring Security auth.\nimport com.alibaba.nacos.common.utils.StringUtils;\n\nif (StringUtils.isBlank(username)) {\n    throw new UsernameNotFoundException(\"username is blank\");\n}\n// With caching off, this hits the DB directly:\nif (userService.getUser(username) == null) {\n    throw new UsernameNotFoundException(\"User \" + username + \" not found\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    UserDetails ud = userService.loadUserByUsername(username);\n} catch (UsernameNotFoundException e) {\n    // expected for unknown users -> standard auth failure (401)\n    throw new org.springframework.security.authentication.BadCredentialsException(\"invalid credentials\");\n}","preventionTips":["Map UsernameNotFoundException to a generic invalid-credentials error (avoid leaking existence).","If caching is on, allow time for reload() or force a refresh on a fresh node.","Confirm the username exists via /user/list when debugging.","Query the correct namespace/environment."],"tags":["auth","user-management","login","spring-security"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}