{"record":{"id":"0276d9a760b59efb","repo":"wuyouzhuguli/SpringAll","slug":"error-0276d9","errorCode":null,"errorMessage":"未找到与该手机号对应的用户","messagePattern":"未找到与该手机号对应的用户","errorType":"exception","errorClass":"InternalAuthenticationServiceException","httpStatus":null,"severity":"error","filePath":"59.Spring-Security-SessionManager/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/59.Spring-Security-SessionManager/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java#L2-L38","documentation":"Thrown by SmsAuthenticationProvider.authenticate as InternalAuthenticationServiceException when userDetailService.loadUserByUsername(mobile) returns null. It signals that no account is registered for the given mobile number. Note: Spring's UserDetailsService contract typically throws UsernameNotFoundException rather than returning null, so whether this branch is reachable depends on the custom UserDetailService implementation.","triggerScenarios":"An SMS login where the submitted mobile number is not registered in the user store; the UserDetailService returns null instead of throwing on a missing user; the mobile number format differs from how it was stored.","commonSituations":"User mistypes the mobile number; the number was never provisioned; UserDetailService normalizes differently (e.g., +86 prefix vs. bare digits); test environment missing seed users.","solutions":["Make UserDetailService.loadUserByUsername throw UsernameNotFoundException for unknown users (Spring idiomatic) instead of returning null, and handle that distinctly.","Verify the mobile number is normalized consistently between registration and lookup.","Ensure the user store actually contains a record for the submitted mobile.","Return a friendly 'mobile not registered' message to the client from the failure handler."],"exampleFix":"// before\nif (userDetails == null)\n    throw new InternalAuthenticationServiceException(\"未找到与该手机号对应的用户\");\n\n// after\n// let loadUserByUsername throw UsernameNotFoundException, and let the provider\n// surface it through AuthenticationFailureHandler instead of a 500.","handlingStrategy":"try-catch","validationCode":"// optional pre-check that the mobile is registered before auth\nif (!userService.existsByMobile(mobile)) { throw new UsernameNotFoundException(mobile); }","typeGuard":"// narrow a loaded user before proceeding\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 a friendly 'mobile not registered' response\n}","preventionTips":["Normalize mobile format on both write and lookup.","Throw UsernameNotFoundException for unknown users in the service.","Seed test users in non-prod environments."],"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"}