From 61121eb5079df0cd6d0782d394b00bde49abeddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Wed, 20 Nov 2024 18:58:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E8=B0=83=E6=95=B4=20request.?= =?UTF-8?q?getParameterMap()=E7=AD=89=E4=BD=BF=E7=94=A8=EF=BC=8C=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E4=B8=9C=E6=96=B9=E9=80=9A=E7=AD=89=E5=AE=B9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/util/RepeatBodyRequestWrapper.java | 39 +++++++++++++------ .../pigx/common/log/util/SysLogUtils.java | 5 ++- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RepeatBodyRequestWrapper.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RepeatBodyRequestWrapper.java index 574b0a628..826ea7545 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RepeatBodyRequestWrapper.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/RepeatBodyRequestWrapper.java @@ -28,6 +28,7 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.util.HashMap; import java.util.Map; /** @@ -39,13 +40,13 @@ import java.util.Map; public class RepeatBodyRequestWrapper extends HttpServletRequestWrapper { private final byte[] bodyByteArray; - private final Map parameterMap; public RepeatBodyRequestWrapper(HttpServletRequest request) { super(request); this.bodyByteArray = getByteBody(request); - this.parameterMap = super.getParameterMap(); + // 使用 HashMap 以便后续可以修改 + this.parameterMap = new HashMap<>(request.getParameterMap()); } @Override @@ -60,17 +61,17 @@ public class RepeatBodyRequestWrapper extends HttpServletRequestWrapper { return new ServletInputStream() { @Override public boolean isFinished() { - return false; + return byteArrayInputStream.available() == 0; } @Override public boolean isReady() { - return false; + return true; // 可以读取 } @Override public void setReadListener(ReadListener readListener) { - // doNoting + // doNothing } @Override @@ -84,20 +85,34 @@ public class RepeatBodyRequestWrapper extends HttpServletRequestWrapper { byte[] body = new byte[0]; try { body = StreamUtils.copyToByteArray(request.getInputStream()); - } - catch (IOException e) { + } catch (IOException e) { log.error("解析流中数据异常", e); } return body; } - /** - * 重写 getParameterMap() 方法 解决 undertow 中流被读取后,会进行标记,从而导致无法正确获取 body 中的表单数据的问题 - * @return Map parameterMap - */ @Override public Map getParameterMap() { - return this.parameterMap; + return this.parameterMap; // 返回可变的 parameterMap } + /** + * 设置新的参数映射 + * @param parameterMap 新的参数映射 + */ + public void setParameterMap(Map parameterMap) { + this.parameterMap.clear(); + this.parameterMap.putAll(parameterMap); + } + + @Override + public String getParameter(String name) { + String[] values = parameterMap.get(name); + return (values != null && values.length > 0) ? values[0] : null; + } + + @Override + public String[] getParameterValues(String name) { + return parameterMap.get(name); + } } diff --git a/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java b/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java index 5d21ff151..c42d1bc24 100755 --- a/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java +++ b/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java @@ -40,6 +40,7 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -66,7 +67,9 @@ public class SysLogUtils { // get 参数脱敏 PigxLogProperties logProperties = SpringUtil.getBean(PigxLogProperties.class); - Map paramsMap = MapUtil.removeAny(request.getParameterMap(), + + // 修复request.getParameterMap()其他容器不允许改,tomcat会报错,当然undertow默认可以的 + Map paramsMap = MapUtil.removeAny(new HashMap<>(request.getParameterMap()), ArrayUtil.toArray(logProperties.getExcludeFields(), String.class)); sysLog.setParams(HttpUtil.toParams(paramsMap)); return sysLog;