From 9b68013b2af87b9c809c4637299abd929bc73510 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 13 Sep 2024 21:13:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=AF=BC=E5=85=A5=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=B6=88=E6=81=AF=E5=AF=B9=E5=90=8D=E7=A7=B0=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/ExceptionUtil.java | 28 +++++++++++++++++++ .../service/impl/SysUserServiceImpl.java | 10 ++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExceptionUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExceptionUtil.java index 50b10fe89..f6e25fd33 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExceptionUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExceptionUtil.java @@ -36,4 +36,32 @@ public class ExceptionUtil } return StringUtils.defaultString(msg); } + + /** + * 检测异常e被触发的原因是不是因为异常cause。 + * + * @param e 捕获的异常。 + * @param cause 异常触发原因。 + * @return 如果异常e是由cause类异常触发,则返回true;否则返回false。 + */ + public static boolean isCausedBy(final Throwable e, final Class cause) + { + if (cause.isAssignableFrom(e.getClass())) + { + return true; + } + else + { + Throwable t = e.getCause(); + while (t != null && t != e) + { + if (cause.isAssignableFrom(t.getClass())) + { + return true; + } + t = t.getCause(); + } + return false; + } + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 2c2ebccde..cee07fb28 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -3,6 +3,7 @@ package com.ruoyi.system.service.impl; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import javax.validation.ConstraintViolationException; import javax.validation.Validator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,9 +17,11 @@ import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.ExceptionUtil; import com.ruoyi.common.utils.ShiroUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.bean.BeanValidators; +import com.ruoyi.common.utils.html.EscapeUtil; import com.ruoyi.common.utils.security.Md5Utils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.system.domain.SysPost; @@ -529,7 +532,12 @@ public class SysUserServiceImpl implements ISysUserService catch (Exception e) { failureNum++; - String msg = "
" + failureNum + "、账号 " + user.getLoginName() + " 导入失败:"; + String loginName = user.getLoginName(); + if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class)) + { + loginName = EscapeUtil.clean(loginName); + } + String msg = "
" + failureNum + "、账号 " + loginName + " 导入失败:"; failureMsg.append(msg + e.getMessage()); log.error(msg, e); }