wuyouzhuguli/SpringAll · error · InternalAuthenticationServiceException

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

Error message

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

What it means

InternalAuthenticationServiceException thrown by SmsAuthenticationProvider.authenticate (project 64) when userDetailService.loadUserByUsername(mobile) returns null. Identical semantics to error 65 (project 61); the principal is the mobile number and the UserDetailService must resolve it. Same caveat: standard UserDetailsService implementations throw UsernameNotFoundException rather than returning null.

Source

Thrown at 64.Spring-Security-OAuth2-Customize/src/main/java/cc/mrbird/security/validate/smscode/SmsAuthenticationProvider.java:20

import cc.mrbird.security.service.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. Register the mobile in the user store so loadUserByUsername(mobile) resolves.
  2. Ensure UserDetailService filters by the phone/mobile column.
  3. Confirm SmsAuthenticationToken carries the mobile as principal.
  4. Throw UsernameNotFoundException from UserDetailService for unknown users (Spring convention).
Defensive patterns

Strategy: validation

Validate before calling

// Verify the mobile is registered before offering SMS login (if an endpoint exists).
const ok = await fetch(`/users/exists?mobile=${encodeURIComponent(mobile)}`).then(r=>r.json());
if (!ok.exists) { promptRegister(mobile); return; }

Try / catch

try { await smsLogin(mobile, code); }
catch (e) {
  if (/未找到与该手机号对应的用户/.test(e.message)) promptRegister(mobile);
  else handleError(e);
}

Prevention

When it happens

Trigger: Mobile number not registered with the user store; UserDetailService queries by username, not phone; principal mismatch.

Common situations: No user row for the phone; schema phone column not queried; test fixtures missing.

Related errors


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