wuyouzhuguli/SpringAll · error · InternalAuthenticationServiceException

未找到与该手机号对应的用户

Error message

未找到与该手机号对应的用户

What it means

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.

Source

Thrown at 59.Spring-Security-SessionManager/src/main/java/cc/mrbird/validate/smscode/SmsAuthenticationProvider.java:20

import cc.mrbird.security.browser.UserDetailService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

public class SmsAuthenticationProvider implements AuthenticationProvider {

    private UserDetailService userDetailService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        SmsAuthenticationToken authenticationToken = (SmsAuthenticationToken) authentication;
        UserDetails userDetails = userDetailService.loadUserByUsername((String) authenticationToken.getPrincipal());

        if (userDetails == null)
            throw new InternalAuthenticationServiceException("未找到与该手机号对应的用户");

        SmsAuthenticationToken authenticationResult = new SmsAuthenticationToken(userDetails, userDetails.getAuthorities());

        authenticationResult.setDetails(authenticationToken.getDetails());

        return authenticationResult;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return SmsAuthenticationToken.class.isAssignableFrom(aClass);
    }

    public UserDetailService getUserDetailService() {
        return userDetailService;
    }

    public void setUserDetailService(UserDetailService userDetailService) {

View on GitHub (pinned to 614d2578d9)

Solutions

  1. Make UserDetailService.loadUserByUsername throw UsernameNotFoundException for unknown users (Spring idiomatic) instead of returning null, and handle that distinctly.
  2. Verify the mobile number is normalized consistently between registration and lookup.
  3. Ensure the user store actually contains a record for the submitted mobile.
  4. Return a friendly 'mobile not registered' message to the client from the failure handler.

Example fix

// before
if (userDetails == null)
    throw new InternalAuthenticationServiceException("未找到与该手机号对应的用户");

// after
// let loadUserByUsername throw UsernameNotFoundException, and let the provider
// surface it through AuthenticationFailureHandler instead of a 500.
Defensive patterns

Strategy: try-catch

Validate before calling

// optional pre-check that the mobile is registered before auth
if (!userService.existsByMobile(mobile)) { throw new UsernameNotFoundException(mobile); }

Type guard

// narrow a loaded user before proceeding
if (userDetails == null || !userDetails.isEnabled()) {
    throw new InternalAuthenticationServiceException("未找到与该手机号对应的用户");
}

Try / catch

try {
    authenticationManager.authenticate(new SmsAuthenticationToken(mobile));
} catch (InternalAuthenticationServiceException | UsernameNotFoundException e) {
    // map to a friendly 'mobile not registered' response
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of wuyouzhuguli/SpringAll@614d2578d9 (2026-08-14). Data as JSON: /api/errors/0276d9a760b59efb. Report an issue: GitHub.