|
|
@@ -16,6 +16,7 @@ import com.etotem.cfc.service.WechatWebLoginService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import javax.annotation.Resource;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@@ -103,10 +104,31 @@ public class AdminAuthController {
|
|
|
if (dto == null) {
|
|
|
return Result.error(400, "请求参数格式错误,请检查请求体 JSON 或字段类型");
|
|
|
}
|
|
|
- if (dto.getPhone() == null || dto.getCode() == null) {
|
|
|
+ String phone = dto.getPhone();
|
|
|
+ String code = dto.getCode();
|
|
|
+ // 空串 / 纯空白同样视为「缺参」,不能放行到验证码校验后被当成业务失败计次
|
|
|
+ if (isBlank(phone) || isBlank(code)) {
|
|
|
return Result.error(400, "手机号和验证码不能为空");
|
|
|
}
|
|
|
+ // 手机号格式属参数校验 → 400(与 send-code 同口径),非法输入不得进入锁定计数
|
|
|
+ if (!phone.matches("^1[3-9]\\d{9}$")) {
|
|
|
+ return Result.error(400, "手机号格式不正确");
|
|
|
+ }
|
|
|
|
|
|
+ // B-2:登录链路的依赖异常(DB/Redis/下游抖动)不得以 HTTP 5xx + 异常栈形式暴露给调用方;
|
|
|
+ // 完整堆栈只进服务端日志(手机号脱敏),响应侧固定文案,避免内部信息外泄。
|
|
|
+ try {
|
|
|
+ return doLogin(dto, request);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ log.error("管理员验证码登录处理异常: phone={}", maskPhone(phone), e);
|
|
|
+ return Result.error(503, "登录服务暂时不可用,请稍后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证码登录主体:入参已通过参数校验,此处只处理锁定检查、验证码校验、角色判定与 token 签发。
|
|
|
+ */
|
|
|
+ private Result<Map<String, Object>> doLogin(AdminLoginDTO dto, HttpServletRequest request) {
|
|
|
String ip = getClientIP(request);
|
|
|
String lockKey = dto.getPhone() + ":" + ip;
|
|
|
if (loginAttemptService.isBlocked(lockKey)) {
|
|
|
@@ -167,6 +189,16 @@ public class AdminAuthController {
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * B-2:请求体 JSON 语法错误 / 字段类型不匹配(如 phone 传数字)→ 400 信封,
|
|
|
+ * 不回传解析器原始信息与堆栈;与 {@code GlobalExceptionHandler} 同口径,保证独立部署/测试环境一致。
|
|
|
+ */
|
|
|
+ @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
+ public Result<Map<String, Object>> handleUnreadableBody(HttpMessageNotReadableException e) {
|
|
|
+ log.warn("管理员登录请求体解析失败: {}", e.getMessage());
|
|
|
+ return Result.error(400, "请求参数格式错误,请检查请求体 JSON 或字段类型");
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前登录管理员信息
|
|
|
*/
|
|
|
@@ -508,6 +540,19 @@ public class AdminAuthController {
|
|
|
return ip;
|
|
|
}
|
|
|
|
|
|
+ /** 空串/纯空白判定(入参校验前置,避免把空值当业务失败处理) */
|
|
|
+ private static boolean isBlank(String value) {
|
|
|
+ return value == null || value.trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 日志脱敏:手机号仅保留前 3 位与后 4 位,避免日志侧泄露完整号码 */
|
|
|
+ private static String maskPhone(String phone) {
|
|
|
+ if (phone == null || phone.length() < 7) {
|
|
|
+ return "***";
|
|
|
+ }
|
|
|
+ return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 解析用户的后台角色:
|
|
|
* 1. 优先从 roles 数组取第一个后台角色(遗留角色归一化为新枚举);
|