{"record":{"id":"4ea092cc6b4ac590","repo":"wuyouzhuguli/SpringAll","slug":"error-4ea092","errorCode":null,"errorMessage":"未找到与该手机号对应的用户","messagePattern":"未找到与该手机号对应的用户","errorType":"exception","errorClass":"InternalAuthenticationServiceException","httpStatus":null,"severity":"error","filePath":"61.Spring-security-Permission/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java","lineNumber":20,"sourceCode":"\nimport cc.mrbird.security.browser.UserDetailService;\nimport org.springframework.security.authentication.AuthenticationProvider;\nimport org.springframework.security.authentication.InternalAuthenticationServiceException;\nimport org.springframework.security.core.Authentication;\nimport org.springframework.security.core.AuthenticationException;\nimport org.springframework.security.core.userdetails.UserDetails;\n\npublic class SmsAuthenticationProvider implements AuthenticationProvider {\n\n    private UserDetailService userDetailService;\n\n    @Override\n    public Authentication authenticate(Authentication authentication) throws AuthenticationException {\n        SmsAuthenticationToken authenticationToken = (SmsAuthenticationToken) authentication;\n        UserDetails userDetails = userDetailService.loadUserByUsername((String) authenticationToken.getPrincipal());\n\n        if (userDetails == null)\n            throw new InternalAuthenticationServiceException(\"未找到与该手机号对应的用户\");\n\n        SmsAuthenticationToken authenticationResult = new SmsAuthenticationToken(userDetails, userDetails.getAuthorities());\n\n        authenticationResult.setDetails(authenticationToken.getDetails());\n\n        return authenticationResult;\n    }\n\n    @Override\n    public boolean supports(Class<?> aClass) {\n        return SmsAuthenticationToken.class.isAssignableFrom(aClass);\n    }\n\n    public UserDetailService getUserDetailService() {\n        return userDetailService;\n    }\n\n    public void setUserDetailService(UserDetailService userDetailService) {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/wuyouzhuguli/SpringAll/blob/614d2578d9495acf53cc02f2dee9c6131cc5e51a/61.Spring-security-Permission/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java#L2-L38","documentation":"InternalAuthenticationServiceException thrown by SmsAuthenticationProvider.authenticate when userDetailService.loadUserByUsername(mobile) returns null. The principal carried by SmsAuthenticationToken is the mobile number, so the UserDetailService lookup is keyed by phone. Note: most Spring Security UserDetailsService implementations throw UsernameNotFoundException instead of returning null, so reaching this branch implies a custom implementation that swallows missing users.","triggerScenarios":"The submitted mobile number is not registered; the UserDetailService queries by username column, not phone; the principal set in SmsAuthenticationFilter.obtainMobile is not the mobile string the service expects.","commonSituations":"User never signed up / no seed data for that phone; DB schema stores phone in a column the query ignores; SmsAuthenticationFilter wired with the wrong mobileParameter name; test environment missing user fixtures.","solutions":["Register the mobile number in whatever store UserDetailService reads from so loadUserByUsername(mobile) resolves.","Verify UserDetailService actually loads by mobile (the query/filter must match the phone column), not by username.","Confirm SmsAuthenticationToken.getPrincipal() is the mobile string (SmsAuthenticationFilter sets it from obtainMobile).","Prefer throwing UsernameNotFoundException inside UserDetailService for unknown users (Spring convention) so the failure is surfaced at the right layer."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before offering SMS login, verify the mobile is registered (if an endpoint exists).\nconst ok = await fetch(`/users/exists?mobile=${encodeURIComponent(mobile)}`).then(r=>r.json());\nif (!ok.exists) { showError('该手机号未注册'); return; }","typeGuard":null,"tryCatchPattern":"try { await smsLogin(mobile, code); }\ncatch (e) {\n  if (/未找到与该手机号对应的用户/.test(e.message)) { promptRegister(mobile); }\n  else handleError(e);\n}","preventionTips":["Make UserDetailService load by the mobile column.","Throw UsernameNotFoundException (Spring convention) for unknown users instead of returning null.","Seed test users for the mobiles used in QA."],"tags":["spring-security","sms","user-management","authentication"],"backgroundTag":null,"analyzedSha":"614d2578d9495acf53cc02f2dee9c6131cc5e51a","analyzedAt":"2026-08-14T04:40:03.488Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}