macrozheng/mall-learning · error · UsernameNotFoundException

用户名或密码错误

Error message

用户名或密码错误

What it means

Identical to error 0 but in the mall-tiny-05 module: the UserDetailsService bean throws UsernameNotFoundException when no UmsAdmin row matches the username supplied to the authentication manager. Spring's DaoAuthenticationProvider invokes this during every login attempt, so any unknown username surfaces as this error before password checking happens.

Solutions

  1. Confirm the username exists: SELECT * FROM ums_admin WHERE username = '...'
  2. Load the project's seed SQL (mall_tiny schema + data) into the configured database
  3. Verify datasource URL/credentials in application.yml match the intended environment
  4. Create the account via the register API before logging in

Example fix

// before
AdminUserDetails admin = adminService.getAdminByUsername(username);
if (admin != null) { return admin; }
throw new UsernameNotFoundException("用户名或密码错误");
// after
AdminUserDetails admin = adminService.getAdminByUsername(username.trim());
if (admin != null) { return admin; }
log.warn("Unknown username login attempt: {}", username);
throw new UsernameNotFoundException("用户名或密码错误");
Defensive patterns

Strategy: try-catch

Validate before calling

if (!username || !username.trim()) { throw new IllegalArgumentException("用户名不能为空"); }
// server check: SELECT COUNT(*) FROM ums_admin WHERE username = ?

Try / catch

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

Prevention

When it happens

Trigger: Authentication via POST /admin/login with a username absent from the database; adminService.getAdminByUsername(username) returns null.

Common situations: Fresh mall-tiny-05 deployment without importing the SQL seed data, wrong spring.datasource URL/profile, user was deleted or status changed, username typo or extra whitespace from the client.

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

Appendix: source

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