From 6661c016e816113770daabc3bf9d86e95f4e00ff Mon Sep 17 00:00:00 2001 From: rcx <3084692334@qq.com> Date: Tue, 18 Aug 2026 18:08:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=E5=AF=86=E7=A0=81=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7/=E5=AF=86=E7=A0=81=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=97=A0=E6=8F=90=E7=A4=BA=EF=BC=8C=E7=99=BB=E5=BD=95=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=AF=BB=E5=8F=96=E4=B8=8E=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=85=9C=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.SysPasswordService.validate登录重试配置(login/maxRetryCount,lockTime)读取增加try/catch与空值保护,三方服务异常或配置缺失时降级默认值(错5次锁30分钟)并warn日志,不再NPE/NumberFormatException导致msg为空 2.GlobalExceptionHandler的RuntimeException/Exception分支对e.getMessage()为空兜底为'系统繁忙,请稍后重试',保证App端始终能拿到可展示的错误信息 --- .../system/service/SysPasswordService.java | 48 ++++++++++--------- .../handler/GlobalExceptionHandler.java | 6 ++- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/mhd-auth/src/main/java/com/mhd/auth/system/service/SysPasswordService.java b/mhd-auth/src/main/java/com/mhd/auth/system/service/SysPasswordService.java index eded6927d..9c3d126e6 100644 --- a/mhd-auth/src/main/java/com/mhd/auth/system/service/SysPasswordService.java +++ b/mhd-auth/src/main/java/com/mhd/auth/system/service/SysPasswordService.java @@ -28,6 +28,7 @@ import com.mhd.common.redis.service.RedisService; import com.mhd.common.security.utils.password.PasswordUtil; import com.mhd.system.api.RemoteLogService; import com.mhd.system.api.domain.SysLogininforPo; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -39,6 +40,7 @@ import javax.annotation.Resource; * @description: 登录密码方法 * @date 2023/12/26 15:33 **/ +@Slf4j @Component public class SysPasswordService { @@ -76,29 +78,31 @@ public class SysPasswordService if (retryCount == null) { retryCount = 0; } - //TODO 从三方配置表中取值 - SysDepartInterfaceInfoDTO sysDepartInterfaceInfoDTO = new SysDepartInterfaceInfoDTO(); - sysDepartInterfaceInfoDTO.setTopOrganizationId(userPo.getTopOrganizationId()); - sysDepartInterfaceInfoDTO.setInterfaceType("login"); - sysDepartInterfaceInfoDTO.setInterfaceTypeKey("maxRetryCount"); - List sysDepartInterfaceInfoPos = thirdPartyServiceFeign.queryListByFeign(sysDepartInterfaceInfoDTO); - if (sysDepartInterfaceInfoPos.isEmpty()){ - String errMsg = "缺少登录信息配置,请联系管理员"; - recordLogininfor(userPo, Constants.LOGIN_FAIL, errMsg, 1); - throw new ServiceException(errMsg); + //TODO 从三方配置表中取值(失败时用默认值,保证登录错误提示可用) + int maxRetryCount = 5; // 默认:密码错5次锁定 + long lockTime = 30L; // 默认:锁定30分钟 + try { + SysDepartInterfaceInfoDTO dto = new SysDepartInterfaceInfoDTO(); + dto.setTopOrganizationId(userPo.getTopOrganizationId()); + dto.setInterfaceType("login"); + dto.setInterfaceTypeKey("maxRetryCount"); + List pos = thirdPartyServiceFeign.queryListByFeign(dto); + if (pos != null && !pos.isEmpty()) { + maxRetryCount = Integer.parseInt(pos.get(0).getInterfaceTypeValue()); + } else { + log.warn("组织[{}]缺少登录配置 login/maxRetryCount,使用默认值{}", userPo.getTopOrganizationId(), maxRetryCount); + } + dto.setInterfaceTypeKey("lockTime"); + pos = thirdPartyServiceFeign.queryListByFeign(dto); + if (pos != null && !pos.isEmpty()) { + lockTime = Long.parseLong(pos.get(0).getInterfaceTypeValue()); + } else { + log.warn("组织[{}]缺少登录配置 login/lockTime,使用默认值{}", userPo.getTopOrganizationId(), lockTime); + } + } catch (Exception e) { + log.warn("读取登录配置失败,使用默认重试策略", e); } - SysDepartInterfaceInfoPo sysDepartInterfaceInfoPo = sysDepartInterfaceInfoPos.get(0); - Integer maxRetryCount = Integer.valueOf(sysDepartInterfaceInfoPo.getInterfaceTypeValue()); - sysDepartInterfaceInfoDTO.setInterfaceTypeKey("lockTime"); - sysDepartInterfaceInfoPos = thirdPartyServiceFeign.queryListByFeign(sysDepartInterfaceInfoDTO); - if (sysDepartInterfaceInfoPos.isEmpty()){ - String errMsg = "缺少登录信息配置,请联系管理员"; - recordLogininfor(userPo, Constants.LOGIN_FAIL, errMsg, 1); - throw new ServiceException(errMsg); - } - sysDepartInterfaceInfoPo = sysDepartInterfaceInfoPos.get(0); - Long lockTime = Long.valueOf(sysDepartInterfaceInfoPo.getInterfaceTypeValue()); - if (retryCount >= maxRetryCount){ + if (retryCount >= maxRetryCount) { String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime); recordLogininfor(userPo, Constants.LOGIN_FAIL, errMsg, 1); throw new ServiceException(errMsg); diff --git a/mhd-common/mhd-common-security/src/main/java/com/mhd/common/security/handler/GlobalExceptionHandler.java b/mhd-common/mhd-common-security/src/main/java/com/mhd/common/security/handler/GlobalExceptionHandler.java index e166429f3..56ca86d91 100644 --- a/mhd-common/mhd-common-security/src/main/java/com/mhd/common/security/handler/GlobalExceptionHandler.java +++ b/mhd-common/mhd-common-security/src/main/java/com/mhd/common/security/handler/GlobalExceptionHandler.java @@ -88,7 +88,8 @@ public class GlobalExceptionHandler { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); - return AjaxResult.error(e.getMessage()); + String msg = StringUtils.isEmpty(e.getMessage()) ? "系统繁忙,请稍后重试" : e.getMessage(); + return AjaxResult.error(msg); } /** @@ -99,7 +100,8 @@ public class GlobalExceptionHandler { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); - return AjaxResult.error(e.getMessage()); + String msg = StringUtils.isEmpty(e.getMessage()) ? "系统繁忙,请稍后重试" : e.getMessage(); + return AjaxResult.error(msg); } /**