macrozheng/mall-learning · error · UsernameNotFoundException

用户名或密码错误

Error message

用户名或密码错误

What it means

Same family as errors 0-1, in mall-tiny-06: the UserDetailsService lambda throws UsernameNotFoundException because adminService.getAdminByUsername found no matching admin row. This exception is translated by Spring Security's authentication provider into a failed login, surfaced with the generic 'username or password wrong' message.

Solutions

  1. Query ums_admin for the username to confirm it exists
  2. Re-import the module's SQL seed script into the configured database
  3. Check application.yml datasource and profile configuration
  4. Normalize/trim usernames before lookup

Example fix

// before
if (admin != null) { return admin; }
throw new UsernameNotFoundException("用户名或密码错误");
// after
if (admin != null) { return admin; }
log.warn("No admin found for username: {}", username);
throw new UsernameNotFoundException("用户名或密码错误");
Defensive patterns

Strategy: try-catch

Validate before calling

if (username == null || username.isBlank()) throw new IllegalArgumentException("用户名不能为空");

Try / catch

try {
    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (AuthenticationException e) {
    return CommonResult.failed("用户名或密码错误");
}

Prevention

When it happens

Trigger: Login/authentication call with a username that has no row in ums_admin; the null check in the lambda fails and the exception is thrown.

Common situations: Missing seed data after recreating the database, wrong active Spring profile/database, username case mismatch (MySQL collation differences), client sending email instead of username.

Understand the failure class

Background: "User not found", "Invalid user", and "does not exist": what missing-user lookup errors mean across Rocket.Chat, LiteLLM, Phabricator, rustfs, and pnpm — this error's family across 10 libraries.

Related errors


AI-assisted analysis of macrozheng/mall-learning@cd02c000e5 (2026-09-07). Data as JSON: /api/errors/2013554fa8453288. Report an issue: GitHub.

Appendix: source

Thrown at mall-tiny-06/src/main/java/com/macro/mall/tiny/config/MallSecurityConfig.java:31

 * @description 自定义配置,用于配置如何获取用户信息
 * @date 2022/5/20
 * @github https://github.com/macrozheng
 */
@Configuration
public class MallSecurityConfig {

    @Autowired
    private UmsAdminService adminService;

    @Bean
    public UserDetailsService userDetailsService() {
        //获取登录用户信息
        return username -> {
            AdminUserDetails admin = adminService.getAdminByUsername(username);
            if (admin != null) {
                return admin;
            }
            throw new UsernameNotFoundException("用户名或密码错误");
        };
    }
}

View on GitHub (pinned to cd02c000e5)