{"record":{"id":"77099c1386f35c26","repo":"wuyouzhuguli/SpringAll","slug":"error-77099c","errorCode":null,"errorMessage":"未找到与该手机号对应的用户","messagePattern":"未找到与该手机号对应的用户","errorType":"exception","errorClass":"InternalAuthenticationServiceException","httpStatus":null,"severity":"error","filePath":"38.Spring-Security-SmsCode/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/38.Spring-Security-SmsCode/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java#L2-L38","documentation":"Spring Security's InternalAuthenticationServiceException, thrown by SmsAuthenticationProvider.authenticate when userDetailService.loadUserByUsername returns null for the mobile number. InternalAuthenticationServiceException signals a server-side/internal problem rather than a user credential error. Note the Spring convention is for UserDetailsService to throw UsernameNotFoundException on a miss, not return null, so this branch typically indicates a non-conformant custom UserDetailService.","triggerScenarios":"SMS login (POST /login/mobile) for a mobile number not present in the user store, when the configured UserDetailService returns null instead of throwing.","commonSituations":"Custom UserDetailService returns null on miss; the mobile number is not registered; userDetailService bean is the wrong implementation or not injected; DB query for the mobile returns no row.","solutions":["Make UserDetailService.loadUserByUsername throw UsernameNotFoundException (Spring convention) when the mobile is unknown, so it maps to the standard auth-failure flow.","Register the user/mobile in the user store if login should succeed.","Verify the correct UserDetailService bean is wired into SmsAuthenticationProvider.","Confirm the mobile lookup query matches how numbers are stored (formatting, country code)."],"exampleFix":"// before\npublic UserDetails loadUserByUsername(String mobile) {\n    User u = userMapper.findByMobile(mobile);\n    return u;            // returns null -> InternalAuthenticationServiceException\n}\n\n// after\npublic UserDetails loadUserByUsername(String mobile) {\n    User u = userMapper.findByMobile(mobile);\n    if (u == null) {\n        throw new UsernameNotFoundException(\"mobile not found: \" + mobile);\n    }\n    return u;\n}","handlingStrategy":"validation","validationCode":"// before triggering SMS auth, confirm the mobile is registered\nboolean exists = userMapper.existsByMobile(mobile);\nif (!exists) {\n    throw new UsernameNotFoundException(\"mobile not registered\");\n}","typeGuard":null,"tryCatchPattern":"// In the provider, treat null as a not-found rather than an internal error\nUserDetails userDetails = userDetailService.loadUserByUsername(mobile);\nif (userDetails == null) {\n    throw new UsernameNotFoundException(\"未找到与该手机号对应的用户\");\n}","preventionTips":["Implement UserDetailsService to throw UsernameNotFoundException on a miss instead of returning null.","Wire the correct UserDetailService bean into SmsAuthenticationProvider.","Normalize mobile format (trim, country code) consistently at storage and lookup."],"tags":["spring-security","sms-authentication","user-not-found","authentication-provider"],"backgroundTag":null,"analyzedSha":"614d2578d9495acf53cc02f2dee9c6131cc5e51a","analyzedAt":"2026-08-14T04:40:03.488Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}