{"record":{"id":"4bd8cfeefc2f8595","repo":"wuyouzhuguli/SpringAll","slug":"error-4bd8cf","errorCode":null,"errorMessage":"未找到与该手机号对应的用户","messagePattern":"未找到与该手机号对应的用户","errorType":"exception","errorClass":"InternalAuthenticationServiceException","httpStatus":null,"severity":"error","filePath":"60.Spring-Security-Logout/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/60.Spring-Security-Logout/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java#L2-L38","documentation":"Thrown by SmsAuthenticationProvider.authenticate (Logout project) as InternalAuthenticationServiceException when userDetailService.loadUserByUsername(mobile) returns null — no account for that mobile. Reachability depends on the custom UserDetailService, since Spring's contract usually throws UsernameNotFoundException instead of returning null.","triggerScenarios":"SMS login with an unregistered mobile; UserDetailService returns null instead of throwing; mobile normalized differently than stored.","commonSituations":"Typo in the number; user never provisioned; +86 prefix mismatch; test env missing seed data.","solutions":["Have loadUserByUsername throw UsernameNotFoundException for unknown users instead of returning null.","Normalize the mobile number consistently between registration and lookup.","Ensure the user store contains the submitted mobile.","Surface a friendly 'not registered' message via the failure handler."],"exampleFix":"// before\nif (userDetails == null)\n    throw new InternalAuthenticationServiceException(\"未找到与该手机号对应的用户\");\n\n// after\n// throw UsernameNotFoundException in the service; map it to a friendly auth failure.","handlingStrategy":"try-catch","validationCode":"// optional pre-check that the mobile is registered\nif (!userService.existsByMobile(mobile)) { throw new UsernameNotFoundException(mobile); }","typeGuard":"// narrow a loaded user\nif (userDetails == null || !userDetails.isEnabled()) {\n    throw new InternalAuthenticationServiceException(\"未找到与该手机号对应的用户\");\n}","tryCatchPattern":"try {\n    authenticationManager.authenticate(new SmsAuthenticationToken(mobile));\n} catch (InternalAuthenticationServiceException | UsernameNotFoundException e) {\n    // map to friendly 'mobile not registered' response\n}","preventionTips":["Normalize mobile format on write and lookup.","Throw UsernameNotFoundException for unknown users.","Seed test users in non-prod."],"tags":["spring-security","authentication","sms","user-management","java"],"backgroundTag":null,"analyzedSha":"614d2578d9495acf53cc02f2dee9c6131cc5e51a","analyzedAt":"2026-08-14T04:40:03.488Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}