zhuyifan

feat(auth): 添加账号或密码错误的异常处理

- 在 ErrorCodeEnum 中新增 ACCOUNT_OR_PASSWORD_IS_INCORRECT 错误码
- 修改 LoginServiceImpl 中的登录逻辑,使用新的错误码替代原有错误码
-为 ClientEndExceptions.AuthenticationFailure 添加基于 ErrorCodeEnum 的构建方法
- 更新 AuthenticationFailure 构造函数以支持 ErrorCodeEnum 参数
......@@ -21,6 +21,10 @@ public class ClientEndExceptions {
* 权限登陆未认证异常
*/
public static class AuthenticationFailure extends ClientEndException {
private AuthenticationFailure(ErrorCodeEnum errorCodeEnum) {
super(errorCodeEnum.getCode(), errorCodeEnum.getMessage());
}
private AuthenticationFailure(String message) {
super(ErrorCodeEnum.UNAUTHORIZED.getCode(), message);
}
......@@ -28,6 +32,10 @@ public class ClientEndExceptions {
public synchronized static AuthenticationFailure build(String message) {
return new AuthenticationFailure(message);
}
public synchronized static AuthenticationFailure build(ErrorCodeEnum errorCodeEnum) {
return new AuthenticationFailure(errorCodeEnum);
}
}
/**
......
......@@ -66,7 +66,9 @@ public enum ErrorCodeEnum implements BaseEnum {
MP_ACCOUNT_ALREADY_DISABLED(406000024, "账号已禁用"),
DINER_MEAL_SUSPENDED(406000025, "当前就餐人处于停餐状态")
DINER_MEAL_SUSPENDED(406000025, "当前就餐人处于停餐状态"),
ACCOUNT_OR_PASSWORD_IS_INCORRECT(406000026, "账号或密码错误")
;
......
......@@ -135,13 +135,13 @@ public class LoginServiceImpl implements LoginService {
} else if (loginDto.isShouldLoginWithOperatorPhone()) {
final var operator = operatorServiceRpcClient.getCOperatorByMobileOrEmail(loginDto.getOperatorPhone());
if (operator.getId() == 0) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.UNAUTHORIZED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
if (!operator.getPassword().equals(sha1Hash(loginDto.getPassword()))) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
if (operator.getStatus() != 1) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
StpUtil.login(operator.getId(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, operator.getEnterpriseId())
......@@ -151,13 +151,13 @@ public class LoginServiceImpl implements LoginService {
} else {
final var operator = wOperatorServiceRpcClient.getWOperatorByEmail(loginDto.getEmail()).getResponse();
if (operator.getId() == 0) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.UNAUTHORIZED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
if (!operator.getPassword().equals(sha1Hash(loginDto.getPassword()))) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
if (operator.getStatus() != WOperatorStatus.SHOW) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_INCORRECT);
}
StpUtil.login(operator.getId(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, operator.getEnterpriseId())
......