From 7eedf37149b89e6200c5e30abb8dc2575eb6e3d5 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 02 十二月 2021 18:47:18 +0800
Subject: [PATCH] update 优化 pr !118 代码结构
---
ruoyi-common/src/main/java/com/ruoyi/common/filter/XssHttpServletRequestWrapper.java | 78 ++++++++++++++++++---------------------
1 files changed, 36 insertions(+), 42 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/filter/XssHttpServletRequestWrapper.java b/ruoyi-common/src/main/java/com/ruoyi/common/filter/XssHttpServletRequestWrapper.java
index 3c9efb2..a8ffd66 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/filter/XssHttpServletRequestWrapper.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/filter/XssHttpServletRequestWrapper.java
@@ -1,44 +1,41 @@
package com.ruoyi.common.filter;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.http.HtmlUtil;
+import com.ruoyi.common.utils.StringUtils;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
-import org.apache.commons.io.IOUtils;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.html.EscapeUtil;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
/**
* XSS杩囨护澶勭悊
- *
+ *
* @author ruoyi
*/
-public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper
-{
+public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
/**
* @param request
*/
- public XssHttpServletRequestWrapper(HttpServletRequest request)
- {
+ public XssHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
- public String[] getParameterValues(String name)
- {
+ public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
- if (values != null)
- {
+ if (values != null) {
int length = values.length;
String[] escapseValues = new String[length];
- for (int i = 0; i < length; i++)
- {
+ for (int i = 0; i < length; i++) {
// 闃瞲ss鏀诲嚮鍜岃繃婊ゅ墠鍚庣┖鏍�
- escapseValues[i] = EscapeUtil.clean(values[i]).trim();
+ escapseValues[i] = HtmlUtil.cleanHtmlTag(values[i]).trim();
}
return escapseValues;
}
@@ -46,46 +43,44 @@
}
@Override
- public ServletInputStream getInputStream() throws IOException
- {
+ public ServletInputStream getInputStream() throws IOException {
// 闈瀓son绫诲瀷锛岀洿鎺ヨ繑鍥�
- if (!isJsonRequest())
- {
+ if (!isJsonRequest()) {
return super.getInputStream();
}
// 涓虹┖锛岀洿鎺ヨ繑鍥�
- String json = IOUtils.toString(super.getInputStream(), "utf-8");
- if (StringUtils.isEmpty(json))
- {
+ String json = IoUtil.read(super.getInputStream(), StandardCharsets.UTF_8);
+ if (StringUtils.isEmpty(json)) {
return super.getInputStream();
}
// xss杩囨护
- json = EscapeUtil.clean(json).trim();
- final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("utf-8"));
- return new ServletInputStream()
- {
+ json = HtmlUtil.cleanHtmlTag(json).trim();
+ byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
+ final ByteArrayInputStream bis = IoUtil.toStream(jsonBytes);
+ return new ServletInputStream() {
@Override
- public boolean isFinished()
- {
+ public boolean isFinished() {
return true;
}
@Override
- public boolean isReady()
- {
+ public boolean isReady() {
return true;
}
@Override
- public void setReadListener(ReadListener readListener)
- {
+ public int available() throws IOException {
+ return jsonBytes.length;
}
@Override
- public int read() throws IOException
- {
+ public void setReadListener(ReadListener readListener) {
+ }
+
+ @Override
+ public int read() throws IOException {
return bis.read();
}
};
@@ -93,12 +88,11 @@
/**
* 鏄惁鏄疛son璇锋眰
- *
+ *
* @param request
*/
- public boolean isJsonRequest()
- {
+ public boolean isJsonRequest() {
String header = super.getHeader(HttpHeaders.CONTENT_TYPE);
- return MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(header);
+ return StringUtils.startsWithIgnoreCase(header, MediaType.APPLICATION_JSON_VALUE);
}
}
\ No newline at end of file
--
Gitblit v1.9.3