macrozheng/mall-learning · error · UsernameNotFoundException

用户名或密码错误

Error message

用户名或密码错误

What it means

mall-tiny-08's UserDetailsService throws UsernameNotFoundException when the submitted username has no matching admin in the database. Spring Security's DaoAuthenticationProvider catches this internally and reports authentication failure, so the client sees a generic bad-credentials response with this message.

Solutions

  1. SELECT the username from ums_admin to confirm existence
  2. Import the project's schema/seed data into the configured database
  3. Check datasource config and Spring profile
  4. Register the user or insert the default admin row

Example fix

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

Strategy: try-catch

Validate before calling

if (!username?.trim()) return reject('用户名不能为空');
// pre-check (optional): userExists = adminService.getAdminByUsername(username) != null

Try / catch

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

Prevention

When it happens

Trigger: Login with a username absent from ums_admin; adminService.getAdminByUsername returns null and the lambda throws.

Common situations: Fresh environment without imported seed SQL, wrong database/profile, deleted or renamed account, username typo or whitespace from client input.

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/e33f6fbebeaa1950. Report an issue: GitHub.

Appendix: source

Thrown at mall-tiny-08/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)