elunez/eladmin · error · BadRequestException
用户不存在
Error message
用户不存在
What it means
Thrown by UserDetailsServiceImpl.loadUserByUsername (eladmin-system/.../security/service/UserDetailsServiceImpl.java:50) during Spring Security authentication when userService.getLoginData(username) returns null. This is eladmin's user-not-found signal: the username typed at login, or embedded in a JWT being re-resolved, has no matching row in sys_user. Spring's DaoAuthenticationProvider surfaces it as a BadCredentials-style 400 via the global exception handler.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java:50
* @author Zheng Jie
* @date 2018-11-22
*/
@Slf4j
@RequiredArgsConstructor
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
private final UserService userService;
private final RoleService roleService;
private final DataService dataService;
private final UserCacheManager userCacheManager;
@Override
public JwtUserDto loadUserByUsername(String username) {
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
if(jwtUserDto == null){
UserDto user = userService.getLoginData(username);
if (user == null) {
throw new BadRequestException("用户不存在");
} else {
if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!");
}
// 获取用户的权限
List<AuthorityDto> authorities = roleService.buildPermissions(user);
// 初始化JwtUserDto
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), authorities);
// 添加缓存数据
userCacheManager.addUserCache(username, jwtUserDto);
}
}
return jwtUserDto;
}
}
View on GitHub (pinned to 55fbf70595)
Solutions
- Verify the username exists: SELECT * FROM sys_user WHERE username = 'admin' against the datasource the running service actually uses.
- If the table is empty, import sql/eladmin.sql (or at minimum the sys_user row) and re-login.
- If the user was deleted while a session was open, clear the token/localStorage in the front end and log in again.
- Confirm getLoginData's query (findByUsername) matches the identifier you are sending — eladmin authenticates on username, not email.
Defensive patterns
Strategy: validation
Validate before calling
// Before login, confirm the account exists against the same backend
// (or at minimum, treat any 400 from /auth/login as invalid credentials).
await axios.get('/api/users/' + encodeURIComponent(username) + '/exists') // if exposed
// Practically: just handle the 400 gracefully in the login form.
try {
await store.dispatch('login', { username, password });
} catch (e) {
this.$notify.error(e.response && e.response.data.message || '登录失败');
} Try / catch
catch (BadRequestException e) when auth endpoints are involved: map to a friendly '用户名或密码错误' message; do NOT retry automatically — not-found is a permanent client condition.
Prevention
- Seed sys_user from sql/eladmin.sql before first login.
- After deleting a user, invalidate front-end tokens so stale JWTs do not keep re-triggering loadUserByUsername.
- Log the attempted username (never the password) server-side to spot case or whitespace mismatches early.
When it happens
Trigger: POST /auth/login with a misspelled or deleted username; a valid JWT for a user deleted after login (token verification re-invokes loadUserByUsername); logging in with the email field while getLoginData only matches username; test environment pointing at a database whose sys_user table is empty or not seeded by eladmin.sql.
Common situations: Fresh clone where sql/eladmin.sql was never imported so the default 'admin' account does not exist; user soft-deleted via DELETE /users but the browser still holds an old token; case-sensitive username mismatch (Admin vs admin); multiple datasource profiles so login hits the wrong schema.
Related errors
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/6fec0dfba299a0d2.
Report an issue: GitHub.