{"record":{"id":"61106fc835cf2b19","repo":"macrozheng/mall-learning","slug":"error-61106f","errorCode":null,"errorMessage":"用户名或密码错误","messagePattern":"用户名或密码错误","errorType":"exception","errorClass":"UsernameNotFoundException","httpStatus":null,"severity":"error","filePath":"mall-tiny-07/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-07/src/main/java/com/macro/mall/tiny/config/MallSecurityConfig.java#L13-L35","documentation":"In mall-tiny-07 the UserDetailsService bean throws UsernameNotFoundException with message '用户名或密码错误' when getAdminByUsername returns null. This is the standard Spring Security mechanism for signaling an unknown principal during authentication; the generic message intentionally hides whether the user exists.","triggerScenarios":"Any authentication attempt (e.g. POST /admin/login) where the username is not present in the ums_admin table.","commonSituations":"Database not seeded, app connected to wrong schema, user account removed, typo/case-sensitivity in username, frontend form submitting empty string.","solutions":["Verify the admin row exists in ums_admin for the given username","Load seed data (mall_tiny.sql) into the database configured in application.yml","Double-check the active Spring profile and datasource URL","Create the user via the register endpoint"],"exampleFix":"// before\nAdminUserDetails admin = adminService.getAdminByUsername(username);\nif (admin != null) { return admin; }\nthrow new UsernameNotFoundException(\"用户名或密码错误\");\n// after\nAdminUserDetails admin = adminService.getAdminByUsername(username == null ? \"\" : username.trim());\nif (admin != null) { return admin; }\nthrow new UsernameNotFoundException(\"用户名或密码错误\");","handlingStrategy":"try-catch","validationCode":"const username = (form.username || '').trim();\nif (!username) throw new Error('用户名不能为空');","typeGuard":null,"tryCatchPattern":"try {\n    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));\n} catch (BadCredentialsException e) {\n    throw new ApiException(\"用户名或密码错误\");\n}","preventionTips":["Run schema+data migrations so admin users always exist","Pin datasource config per environment and smoke-test login after deploy","Register users through the API instead of manual inserts","Never return 'user not found' to clients — keep the generic message"],"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"}