{"record":{"id":"2ecbb839ce086f50","repo":"macrozheng/mall-learning","slug":"error","errorCode":null,"errorMessage":"用户名或密码错误","messagePattern":"用户名或密码错误","errorType":"exception","errorClass":"UsernameNotFoundException","httpStatus":null,"severity":"error","filePath":"mall-tiny-04/src/main/java/com/macro/mall/tiny/config/MallSecurityConfig.java","lineNumber":31,"sourceCode":" * @description 自定义配置，用于配置如何获取用户信息\n * @date 2022/5/20\n * @github https://github.com/macrozheng\n */\n@Configuration\npublic class MallSecurityConfig {\n\n    @Autowired\n    private UmsAdminService adminService;\n\n    @Bean\n    public UserDetailsService userDetailsService() {\n        //获取登录用户信息\n        return username -> {\n            AdminUserDetails admin = adminService.getAdminByUsername(username);\n            if (admin != null) {\n                return admin;\n            }\n            throw new UsernameNotFoundException(\"用户名或密码错误\");\n        };\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":35,"githubUrl":"https://github.com/macrozheng/mall-learning/blob/cd02c000e57c693e49f1f72986dbfa6e4c952e09/mall-tiny-04/src/main/java/com/macro/mall/tiny/config/MallSecurityConfig.java#L13-L35","documentation":"This is a Spring Security UsernameNotFoundException thrown by the UserDetailsService lambda in MallSecurityConfig when getAdminByUsername returns no admin for the submitted username. The message deliberately says '用户名或密码错误' (username or password wrong) instead of 'user not found' to avoid leaking which accounts exist. It is part of the login authentication flow: Spring DaoAuthenticationProvider calls this UserDetailsService before password matching.","triggerScenarios":"POST /admin/login (or any authenticate call) with a username that does not exist in the ums_admin table; adminService.getAdminByUsername returns null so the lambda throws.","commonSituations":"Typos in username during login, testing against a database where the seed admin user was never inserted, pointing the app at the wrong database/schema so the admin row is missing, case-sensitivity mismatch in stored usernames, or frontend sending an empty/untrimmed username.","solutions":["Verify the ums_admin table contains a row with that exact username (SELECT * FROM ums_admin WHERE username = '...')","Check spring.datasource settings point at the database the seed data (mall_tiny.sql) was loaded into","Register the admin first via the register endpoint or insert the default admin row","Trim/normalize the username in the client before submitting"],"exampleFix":"// before\nAdminUserDetails admin = adminService.getAdminByUsername(username);\nif (admin != null) { return admin; }\nthrow new UsernameNotFoundException(\"用户名或密码错误\");\n// after\nAdminUserDetails admin = adminService.getAdminByUsername(username);\nif (admin != null) { return admin; }\n// ensure the seed user exists; log for diagnosis without leaking detail\nlog.warn(\"Login attempt for unknown user: {}\", username);\nthrow new UsernameNotFoundException(\"用户名或密码错误\");","handlingStrategy":"try-catch","validationCode":"// before calling the API\nconst username = form.username.trim();\nif (!username) { alert('请输入用户名'); }\n// optional server-side existence check (admin-only):\n// SELECT COUNT(*) FROM ums_admin WHERE username = ?","typeGuard":null,"tryCatchPattern":"try {\n    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));\n} catch (BadCredentialsException | UsernameNotFoundException e) {\n    // single generic message: '用户名或密码错误'\n    throw new ApiException(\"用户名或密码错误\");\n}","preventionTips":["Always import the project's seed SQL before first login","Verify datasource URL/profile per environment","Trim and normalize username input client-side","Use the register endpoint to create accounts before logging in","Don't reveal whether username or password was wrong"],"tags":["spring-security","authentication","login","jdbc"],"backgroundTag":"user-not-found","analyzedSha":"cd02c000e57c693e49f1f72986dbfa6e4c952e09","analyzedAt":"2026-09-07T23:16:08.059Z","contentChangedAt":"2026-09-07T23:16:08.059Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}