1、贴皮库上传图片大小限制;2、全局异常捕获文件请求过大类型避免异步写审计日志oom;2、新增编辑贴皮入参校验修正;

This commit is contained in:
gaoqr
2026-07-14 13:53:08 +08:00
parent cd2f1e3f77
commit ac515402c5
12 changed files with 109 additions and 16 deletions
@@ -19,6 +19,7 @@ public class GlobalErrorCodeConstants {
// ========== 客户端错误段 ==========
public static final ErrorCode BAD_REQUEST = new ErrorCode(400, "global.error.bad.request");
public static final ErrorCode UPLOAD_SIZE_EXCEEDED = new ErrorCode(413, "global.error.request.upload.size.exceed");
public static final ErrorCode REQUEST_ORGAN_ID_NOT_EXIST = new ErrorCode(400, "global.error.request.organId.notExist");
public static final ErrorCode NO_PERMISSION_TO_VISIT_ORG = new ErrorCode(400, "global.error.no.permission.to.visit.org");
public static final ErrorCode REQUEST_PARAM_TYPE_ERROR = new ErrorCode(400, "global.error.request.param.type.error");
@@ -28,6 +28,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
@@ -90,6 +91,9 @@ public class GlobalExceptionHandler {
if (ex instanceof HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) {
return httpRequestMethodNotSupportedExceptionHandler(httpRequestMethodNotSupportedException);
}
if (ex instanceof MaxUploadSizeExceededException maxUploadSizeExceededException) {
return maxUploadSizeExceededExceptionHandler(maxUploadSizeExceededException);
}
if (ex instanceof ServiceException serviceException) {
return serviceExceptionHandler(serviceException);
}
@@ -253,6 +257,16 @@ public class GlobalExceptionHandler {
return error;
}
/**
* 处理 multipart 请求超过全局上传限制。此类异常发生在请求体解析阶段,不能进入默认异常日志流程,
* 否则错误日志会尝试读取并序列化整个 multipart 请求体,可能导致内存溢出。
*/
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
public CommonResult maxUploadSizeExceededExceptionHandler(MaxUploadSizeExceededException ex) {
log.warn("[maxUploadSizeExceededExceptionHandler] multipart request exceeds configured upload limit");
return CommonResult.error(UPLOAD_SIZE_EXCEEDED);
}
/**
* 处理数据库DuplicateKeyException,违反唯一约束异常
*
@@ -336,7 +350,7 @@ public class GlobalExceptionHandler {
errorLog.setRequestUrl(request.getRequestURI());
Map<String, Object> requestParams = MapUtil.<String, Object>builder()
.put("query", ServletUtils.getParamMap(request))
.put("body", ServletUtils.getBody(request)).build();
.put("body", isMultipartRequest(request) ? "[multipart body omitted]" : ServletUtils.getBody(request)).build();
errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
errorLog.setRequestMethod(request.getMethod());
errorLog.setUserAgent(ServletUtils.getUserAgent(request));
@@ -344,6 +358,11 @@ public class GlobalExceptionHandler {
errorLog.setExceptionTime(LocalDateTime.now());
}
private boolean isMultipartRequest(HttpServletRequest request) {
String contentType = request.getContentType();
return contentType != null && contentType.regionMatches(true, 0, "multipart/", 0, "multipart/".length());
}
/**
* 处理 Table 不存在的异常情况
*