From 109c64e7c20e044ee3fa650f0d0599aeb64633e8 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 13 Mar 2020 22:14:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9B=BF=E6=8D=A2=E8=A1=A8?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/filter/RepeatableFilter.java | 15 ++++--- .../filter/RepeatedlyRequestWrapper.java | 44 +++++++------------ .../ruoyi/project/tool/gen/util/GenUtils.java | 2 +- 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatableFilter.java b/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatableFilter.java index c334814..a1125e3 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatableFilter.java +++ b/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatableFilter.java @@ -9,8 +9,6 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import com.ruoyi.common.enums.HttpMethod; - /** * Repeatable 过滤器 * @@ -28,16 +26,19 @@ public class RepeatableFilter implements Filter public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletRequest req = (HttpServletRequest) request; - if (HttpMethod.PUT.name().equals(req.getMethod()) || HttpMethod.POST.name().equals(req.getMethod())) + ServletRequest requestWrapper = null; + if (request instanceof HttpServletRequest) { - RepeatedlyRequestWrapper repeatedlyRequest = new RepeatedlyRequestWrapper((HttpServletRequest) request); - chain.doFilter(repeatedlyRequest, response); + requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) request, response); } - else + if (null == requestWrapper) { chain.doFilter(request, response); } + else + { + chain.doFilter(requestWrapper, response); + } } @Override diff --git a/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatedlyRequestWrapper.java b/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatedlyRequestWrapper.java index f059d37..bd7097f 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatedlyRequestWrapper.java +++ b/ruoyi/src/main/java/com/ruoyi/common/filter/RepeatedlyRequestWrapper.java @@ -4,12 +4,12 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; -import java.nio.charset.Charset; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; -import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.http.HttpHelper; /** * 构建可重复读取inputStream的request @@ -20,10 +20,13 @@ public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper { private final byte[] body; - public RepeatedlyRequestWrapper(HttpServletRequest request) throws IOException + public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException { super(request); - body = readBytes(request.getReader(), "utf-8"); + request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding("UTF-8"); + + body = HttpHelper.getBodyString(request).getBytes("UTF-8"); } @Override @@ -35,50 +38,35 @@ public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper @Override public ServletInputStream getInputStream() throws IOException { + final ByteArrayInputStream bais = new ByteArrayInputStream(body); + return new ServletInputStream() { @Override - public boolean isFinished() + public int read() throws IOException { - return false; + return bais.read(); } @Override - public boolean isReady() + public boolean isFinished() { return false; } @Override - public void setReadListener(ReadListener listener) + public boolean isReady() { - + return false; } @Override - public int read() throws IOException + public void setReadListener(ReadListener readListener) { - return bais.read(); + } }; } - - /** - * 通过BufferedReader和字符编码集转换成byte数组 - */ - private byte[] readBytes(BufferedReader br, String encoding) throws IOException - { - String str = null, retStr = ""; - while ((str = br.readLine()) != null) - { - retStr += str; - } - if (StringUtils.isNotBlank(retStr)) - { - return retStr.getBytes(Charset.forName(encoding)); - } - return null; - } } diff --git a/ruoyi/src/main/java/com/ruoyi/project/tool/gen/util/GenUtils.java b/ruoyi/src/main/java/com/ruoyi/project/tool/gen/util/GenUtils.java index 48bbe0b..047cd2d 100644 --- a/ruoyi/src/main/java/com/ruoyi/project/tool/gen/util/GenUtils.java +++ b/ruoyi/src/main/java/com/ruoyi/project/tool/gen/util/GenUtils.java @@ -180,7 +180,7 @@ public class GenUtils */ public static String replaceFirst(String replacementm, String[] searchList) { - String text = StringUtils.EMPTY; + String text = replacementm; for (String searchString : searchList) { if (replacementm.startsWith(searchString))