paascloud/paascloud-master · error · UsernameNotFoundException

UsernameNotFoundException(username)

Error message

UsernameNotFoundException(username)

What it means

DefaultUserDetailsServiceImpl is a placeholder implementation of Spring Security's UserDetailsService that always throws UsernameNotFoundException(username). It lets the security core compile without a real user store; hitting it means no application-specific UserDetailsService bean is registered, so username/password (and SMS) login can never authenticate.

Solutions

  1. Implement UserDetailsService.loadUserByUsername to load users (including encoded password and authorities).
  2. Annotate it @Service (or expose via @Bean) so it replaces DefaultUserDetailsServiceImpl.
  3. Check component-scan base packages include your implementation's package.
  4. Verify DaoAuthenticationProvider is wired to your bean, not the default.
  5. Never return null — throw UsernameNotFoundException for unknown users.

Example fix

// before
// missing implementation, default stub throws
// after
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) {
        return userRepository.findByUsername(username)
            .map(SecurityUser::new)
            .orElseThrow(() -> new UsernameNotFoundException(username));
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (userDetailsService instanceof DefaultUserDetailsServiceImpl) {
    throw new IllegalStateException("Configure a real UserDetailsService before enabling login");
}

Try / catch

try {
    UserDetails u = userDetailsService.loadUserByUsername(username);
} catch (UsernameNotFoundException e) {
    log.error("UserDetailsService not implemented or user missing: {}", e.getMessage());
    throw new BadCredentialsException("Invalid username or password");
}

Prevention

When it happens

Trigger: Any form/SMS login authentication flow invoking loadUserByUsername while the application has not defined its own UserDetailsService implementation, or the implementation is not registered as a bean so the default stub is injected.

Common situations: Fresh integration of paascloud-security modules; the real UserDetailsService exists but lacks @Service or is outside component-scan; multiple UserDetailsService beans cause the default to be injected unexpectedly; using the wrong module (core demo) in production.

Understand the failure class

Background: "NotImplementedError: Subclasses should override this method" / "must be implemented" — abstract method errors explained — this error's family across 40 libraries.

Related errors


AI-assisted analysis of paascloud/paascloud-master@781281a950 (2026-09-10). Data as JSON: /api/errors/551a180d770141ad. Report an issue: GitHub.

Appendix: source

Thrown at paascloud-common/paascloud-security-core/src/main/java/com/paascloud/security/core/authentication/DefaultUserDetailsServiceImpl.java:29

 *
 * @author paascloud.net @gmail.com
 */
@Slf4j
public class DefaultUserDetailsServiceImpl implements UserDetailsService {

	/**
	 * Load user by username user details.
	 *
	 * @param username the username
	 *
	 * @return the user details
	 *
	 * @throws UsernameNotFoundException the username not found exception
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		log.warn("请配置 UserDetailsService 接口的实现.");
		throw new UsernameNotFoundException(username);
	}

}

View on GitHub (pinned to 781281a950)