增加用户登录账号密码不对的准确错误提示
This commit is contained in:
parent
79177878fa
commit
6655f120de
@ -167,6 +167,14 @@
|
|||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>32.1.3-jre</version>
|
<version>32.1.3-jre</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.flywaydb</groupId>
|
||||||
|
<artifactId>flyway-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.flywaydb</groupId>
|
||||||
|
<artifactId>flyway-mysql</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -13,20 +13,11 @@ public enum ResponseCode {
|
|||||||
NOT_FOUND(404, "response.not.found"),
|
NOT_FOUND(404, "response.not.found"),
|
||||||
CONFLICT(409, "response.conflict"),
|
CONFLICT(409, "response.conflict"),
|
||||||
|
|
||||||
// 业务错误码
|
|
||||||
TENANT_NOT_FOUND(1001, "tenant.not.found"),
|
|
||||||
DATA_NOT_FOUND(1002, "data.not.found"),
|
|
||||||
|
|
||||||
// 系统异常 (1开头)
|
// 系统异常 (1开头)
|
||||||
OPTIMISTIC_LOCK_ERROR(1003, "system.optimistic.lock.error"), // 乐观锁异常
|
OPTIMISTIC_LOCK_ERROR(1001, "system.optimistic.lock.error"), // 乐观锁异常
|
||||||
PESSIMISTIC_LOCK_ERROR(1004, "system.pessimistic.lock.error"), // 悲观锁异常
|
PESSIMISTIC_LOCK_ERROR(1002, "system.pessimistic.lock.error"), // 悲观锁异常
|
||||||
CONCURRENT_UPDATE_ERROR(1005, "system.concurrent.update.error"), // 并发更新异常
|
CONCURRENT_UPDATE_ERROR(1003, "system.concurrent.update.error"), // 并发更新异常
|
||||||
RETRY_EXCEEDED_ERROR(1006, "system.retry.exceeded.error"), // 重试次数超限异常
|
RETRY_EXCEEDED_ERROR(1004, "system.retry.exceeded.error"), // 重试次数超限异常
|
||||||
|
|
||||||
// 用户相关错误码(2开头)
|
|
||||||
USER_NOT_FOUND(2001, "user.not.found"),
|
|
||||||
USERNAME_EXISTS(2002, "user.username.exists"),
|
|
||||||
EMAIL_EXISTS(2003, "user.email.exists"),
|
|
||||||
|
|
||||||
// 依赖注入相关错误 (1100-1199)
|
// 依赖注入相关错误 (1100-1199)
|
||||||
DEPENDENCY_INJECTION_SERVICE_NOT_FOUND(1100, "dependency.injection.service.not.found"),
|
DEPENDENCY_INJECTION_SERVICE_NOT_FOUND(1100, "dependency.injection.service.not.found"),
|
||||||
@ -34,13 +25,20 @@ public enum ResponseCode {
|
|||||||
DEPENDENCY_INJECTION_CONVERTER_NOT_FOUND(1102, "dependency.injection.converter.not.found"),
|
DEPENDENCY_INJECTION_CONVERTER_NOT_FOUND(1102, "dependency.injection.converter.not.found"),
|
||||||
DEPENDENCY_INJECTION_ENTITYPATH_FAILED(1103, "dependency.injection.entitypath.failed"),
|
DEPENDENCY_INJECTION_ENTITYPATH_FAILED(1103, "dependency.injection.entitypath.failed"),
|
||||||
|
|
||||||
|
// 业务异常 (2开头)
|
||||||
|
TENANT_NOT_FOUND(2001, "tenant.not.found"),
|
||||||
|
DATA_NOT_FOUND(2002, "data.not.found"),
|
||||||
|
USER_NOT_FOUND(2003, "user.not.found"),
|
||||||
|
USERNAME_EXISTS(2004, "user.username.exists"),
|
||||||
|
EMAIL_EXISTS(2005, "user.email.exists"),
|
||||||
|
LOGIN_ERROR(2006, "user.login.error"),
|
||||||
|
|
||||||
// JWT相关错误码 (2100-2199)
|
// JWT相关错误码 (2100-2199)
|
||||||
JWT_EXPIRED(2100, "jwt.token.expired"),
|
JWT_EXPIRED(2100, "jwt.token.expired"),
|
||||||
JWT_INVALID(2101, "jwt.token.invalid"),
|
JWT_INVALID(2101, "jwt.token.invalid"),
|
||||||
JWT_MISSING(2102, "jwt.token.missing");
|
JWT_MISSING(2102, "jwt.token.missing");
|
||||||
|
|
||||||
private final int code;
|
private final int code;
|
||||||
|
|
||||||
private final String messageKey; // 国际化消息key
|
private final String messageKey; // 国际化消息key
|
||||||
|
|
||||||
ResponseCode(int code, String messageKey) {
|
ResponseCode(int code, String messageKey) {
|
||||||
|
|||||||
@ -22,12 +22,8 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(BusinessException.class)
|
@ExceptionHandler(BusinessException.class)
|
||||||
public Response<?> handleBusinessException(BusinessException e) {
|
public Response<?> handleBusinessException(BusinessException e) {
|
||||||
String message = messageSource.getMessage(
|
log.warn("Business error occurred: {}", e.getErrorCode().getMessageKey(), e);
|
||||||
e.getErrorCode().getMessageKey(),
|
return Response.error(e.getErrorCode());
|
||||||
e.getArgs(),
|
|
||||||
LocaleContextHolder.getLocale()
|
|
||||||
);
|
|
||||||
return Response.error(e.getErrorCode(), message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(SystemException.class)
|
@ExceptionHandler(SystemException.class)
|
||||||
@ -36,6 +32,12 @@ public class GlobalExceptionHandler {
|
|||||||
return Response.error(ResponseCode.ERROR);
|
return Response.error(ResponseCode.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(org.springframework.security.authentication.BadCredentialsException.class)
|
||||||
|
public Response<?> handleBadCredentialsException(org.springframework.security.authentication.BadCredentialsException e) {
|
||||||
|
log.warn("Login failed: Bad credentials", e);
|
||||||
|
return Response.error(ResponseCode.LOGIN_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public Response<?> handleException(Exception e) {
|
public Response<?> handleException(Exception e) {
|
||||||
log.error("Unexpected error occurred", e);
|
log.error("Unexpected error occurred", e);
|
||||||
|
|||||||
@ -41,6 +41,9 @@ public class UserServiceImpl extends BaseServiceImpl<User, UserDTO, Long> implem
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private AuthenticationManager authenticationManager;
|
private AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IUserRepository userRepository;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private JwtTokenUtil jwtTokenUtil;
|
private JwtTokenUtil jwtTokenUtil;
|
||||||
@ -78,27 +81,22 @@ public class UserServiceImpl extends BaseServiceImpl<User, UserDTO, Long> implem
|
|||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@Audited(action = "USER_LOGIN", detail = "登录")
|
@Audited(action = "USER_LOGIN", detail = "登录")
|
||||||
public LoginResponse login(LoginRequest request) {
|
public LoginResponse login(LoginRequest request) {
|
||||||
// Authentication authentication = authenticationManager.authenticate(
|
Authentication authentication = authenticationManager.authenticate(
|
||||||
// new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword())
|
new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword())
|
||||||
// );
|
);
|
||||||
//
|
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||||
// UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
String token = jwtTokenUtil.generateToken(userDetails);
|
||||||
// String token = jwtTokenUtil.generateToken(userDetails);
|
User user = userRepository.findByUsernameAndDeletedFalse(userDetails.getUsername())
|
||||||
//
|
.orElseThrow(() -> new BusinessException(ResponseCode.USER_NOT_FOUND));
|
||||||
// User user = userRepository.findByUsernameAndDeletedFalse(userDetails.getUsername())
|
LoginResponse response = new LoginResponse();
|
||||||
// .orElseThrow(() -> new BusinessException(ResponseCode.USER_NOT_FOUND));
|
response.setId(user.getId());
|
||||||
//
|
response.setUsername(user.getUsername());
|
||||||
// LoginResponse response = new LoginResponse();
|
response.setNickname(user.getNickname());
|
||||||
// response.setId(user.getId());
|
response.setEmail(user.getEmail());
|
||||||
// response.setUsername(user.getUsername());
|
response.setPhone(user.getPhone());
|
||||||
// response.setNickname(user.getNickname());
|
response.setToken(token);
|
||||||
// response.setEmail(user.getEmail());
|
log.info("用户 {} ({}) 登录成功", user.getUsername(), user.getNickname());
|
||||||
// response.setPhone(user.getPhone());
|
return response;
|
||||||
// response.setToken(token);
|
|
||||||
//
|
|
||||||
// log.info("用户 {} ({}) 登录成功", user.getUsername(), user.getNickname());
|
|
||||||
// return response;
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
-- 创建租户表
|
||||||
|
CREATE TABLE IF NOT EXISTS sys_tenant (
|
||||||
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
create_by VARCHAR(255) NULL,
|
||||||
|
create_time DATETIME(6) NULL,
|
||||||
|
deleted BIT NOT NULL DEFAULT 0,
|
||||||
|
update_by VARCHAR(255) NULL,
|
||||||
|
update_time DATETIME(6) NULL,
|
||||||
|
version INT NOT NULL DEFAULT 0,
|
||||||
|
address VARCHAR(255) NULL,
|
||||||
|
code VARCHAR(255) NOT NULL,
|
||||||
|
contact_name VARCHAR(255) NULL,
|
||||||
|
contact_phone VARCHAR(255) NULL,
|
||||||
|
email VARCHAR(255) NULL,
|
||||||
|
enabled BIT NOT NULL DEFAULT 1,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
CONSTRAINT UK_tenant_code UNIQUE (code)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 创建用户表
|
||||||
|
CREATE TABLE IF NOT EXISTS sys_user (
|
||||||
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
create_by VARCHAR(255) NULL,
|
||||||
|
create_time DATETIME(6) NULL,
|
||||||
|
deleted BIT NOT NULL DEFAULT 0,
|
||||||
|
update_by VARCHAR(255) NULL,
|
||||||
|
update_time DATETIME(6) NULL,
|
||||||
|
version INT NOT NULL DEFAULT 0,
|
||||||
|
email VARCHAR(255) NULL,
|
||||||
|
enabled BIT NOT NULL DEFAULT 1,
|
||||||
|
nickname VARCHAR(50) NULL,
|
||||||
|
password VARCHAR(255) NOT NULL,
|
||||||
|
phone VARCHAR(255) NULL,
|
||||||
|
username VARCHAR(255) NOT NULL,
|
||||||
|
CONSTRAINT UK_user_username UNIQUE (username)
|
||||||
|
);
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
-- 插入初始租户
|
||||||
|
INSERT INTO sys_tenant
|
||||||
|
(create_by, create_time, deleted, update_by, update_time, version,
|
||||||
|
address, code, contact_name, contact_phone, email, enabled, name)
|
||||||
|
VALUES
|
||||||
|
('system', NOW(), 0, 'system', NOW(), 0,
|
||||||
|
'北京市朝阳区xxx街道', 'default', '张三', '13900000001', 'default@example.com', 1, '默认租户');
|
||||||
|
|
||||||
|
-- 插入管理员用户
|
||||||
|
INSERT INTO sys_user
|
||||||
|
(create_by, create_time, deleted, update_by, update_time, version,
|
||||||
|
email, enabled, nickname, password, phone, username)
|
||||||
|
VALUES
|
||||||
|
('system', NOW(), 0, 'system', NOW(), 0,
|
||||||
|
'admin@example.com', 1, '系统管理员',
|
||||||
|
'$2a$10$mW/yJPHjyueQ1g82qWXg8eYqyUVNxFQPagkUvqtWPhKhqB8Z3Vw2y',
|
||||||
|
'13800138000', 'admin');
|
||||||
@ -16,6 +16,7 @@ data.not.found=找不到ID为{0}的{1}
|
|||||||
user.not.found=用户不存在
|
user.not.found=用户不存在
|
||||||
user.username.exists=用户名已存在
|
user.username.exists=用户名已存在
|
||||||
user.email.exists=邮箱已存在
|
user.email.exists=邮箱已存在
|
||||||
|
user.login.error=用户名或密码错误
|
||||||
|
|
||||||
# 系统异常消息
|
# 系统异常消息
|
||||||
system.optimistic.lock.error=数据已被其他用户修改,请刷新后重试
|
system.optimistic.lock.error=数据已被其他用户修改,请刷新后重试
|
||||||
|
|||||||
@ -16,6 +16,7 @@ data.not.found={0} with id {1} not found
|
|||||||
user.not.found=User not found
|
user.not.found=User not found
|
||||||
user.username.exists=Username already exists
|
user.username.exists=Username already exists
|
||||||
user.email.exists=Email already exists
|
user.email.exists=Email already exists
|
||||||
|
user.login.error=Invalid username or password
|
||||||
|
|
||||||
# System Exception Messages
|
# System Exception Messages
|
||||||
system.optimistic.lock.error=Data has been modified by another user, please refresh and try again
|
system.optimistic.lock.error=Data has been modified by another user, please refresh and try again
|
||||||
|
|||||||
@ -16,6 +16,7 @@ data.not.found=数据不存在
|
|||||||
user.not.found=用户不存在
|
user.not.found=用户不存在
|
||||||
user.username.exists=用户名已存在
|
user.username.exists=用户名已存在
|
||||||
user.email.exists=邮箱已存在
|
user.email.exists=邮箱已存在
|
||||||
|
user.login.error=用户名或密码错误
|
||||||
|
|
||||||
# 系统异常消息
|
# 系统异常消息
|
||||||
system.optimistic.lock.error=数据已被其他用户修改,请刷新后重试
|
system.optimistic.lock.error=数据已被其他用户修改,请刷新后重试
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user