|
|
|
@ -1,101 +1,118 @@
|
|
|
|
|
package com.fuint.common.filter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fuint.framework.exception.BusinessRuntimeException;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
|
|
import javax.servlet.annotation.WebFilter;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@WebFilter(filterName = "CharsetFilter",urlPatterns = "/*")
|
|
|
|
|
public class SpecialCharFilter implements Filter {
|
|
|
|
|
|
|
|
|
|
private static final String SQL_REGX = "[\\\\^$*+?{}()=&;%+\\[\\].|]";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException, IOException {
|
|
|
|
|
HttpServletRequest req = (HttpServletRequest) servletRequest;
|
|
|
|
|
|
|
|
|
|
// 防止流读取一次后就没有了, 所以需要将流继续写出去
|
|
|
|
|
MyRequestWrapper requestWrapper = new MyRequestWrapper(req);
|
|
|
|
|
|
|
|
|
|
// 获取请求参数
|
|
|
|
|
Map<String, Object> paramsMaps = new TreeMap<>();
|
|
|
|
|
if ("POST".equals(req.getMethod().toUpperCase())) {
|
|
|
|
|
String body = requestWrapper.getBody();
|
|
|
|
|
paramsMaps = JSONObject.parseObject(body, TreeMap.class);
|
|
|
|
|
} else {
|
|
|
|
|
Map<String, String[]> parameterMap = requestWrapper.getParameterMap();
|
|
|
|
|
Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
|
|
|
|
|
for (Map.Entry<String, String[]> next : entries) {
|
|
|
|
|
paramsMaps.put(next.getKey(), next.getValue()[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验SQL注入
|
|
|
|
|
if (ObjectUtil.isNotEmpty(paramsMaps)) {
|
|
|
|
|
for (Object o : paramsMaps.entrySet()) {
|
|
|
|
|
Map.Entry entry = (Map.Entry) o;
|
|
|
|
|
Object value = entry.getValue();
|
|
|
|
|
if (value != null) {
|
|
|
|
|
boolean isValid = checkSqlInject(value.toString(), servletResponse);
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chain.doFilter(requestWrapper, servletResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取request请求body中参数
|
|
|
|
|
public static String getBodyString(BufferedReader br) {
|
|
|
|
|
String inputLine;
|
|
|
|
|
String str = "";
|
|
|
|
|
try {
|
|
|
|
|
while ((inputLine = br.readLine()) != null) {
|
|
|
|
|
str += inputLine;
|
|
|
|
|
}
|
|
|
|
|
br.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("IOException: " + e);
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查SQL注入
|
|
|
|
|
*
|
|
|
|
|
* @param value 参数值
|
|
|
|
|
* @param servletResponse 相应实例
|
|
|
|
|
* @throws IOException IO异常
|
|
|
|
|
*/
|
|
|
|
|
private boolean checkSqlInject(String value, ServletResponse servletResponse) throws IOException {
|
|
|
|
|
if (null != value) {
|
|
|
|
|
String output = value.replaceAll(SQL_REGX, "");
|
|
|
|
|
if (output.length()<value.length()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//package com.fuint.common.filter;
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
//import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
//import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
//import com.fuint.framework.exception.BusinessRuntimeException;
|
|
|
|
|
//import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
//import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
//import org.springframework.stereotype.Component;
|
|
|
|
|
//import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
|
|
|
|
//import org.springframework.web.servlet.HandlerExceptionResolver;
|
|
|
|
|
//
|
|
|
|
|
//import javax.servlet.*;
|
|
|
|
|
//import javax.servlet.annotation.WebFilter;
|
|
|
|
|
//import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
//import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
//import java.io.BufferedReader;
|
|
|
|
|
//import java.io.IOException;
|
|
|
|
|
//import java.util.Map;
|
|
|
|
|
//import java.util.Set;
|
|
|
|
|
//import java.util.TreeMap;
|
|
|
|
|
//import java.util.stream.Collectors;
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//@WebFilter(filterName = "CharsetFilter", urlPatterns = "/*")
|
|
|
|
|
//@Component
|
|
|
|
|
//public class SpecialCharFilter implements Filter {
|
|
|
|
|
//
|
|
|
|
|
// private static final String SQL_REGX = "[=]";
|
|
|
|
|
// @Qualifier("handlerExceptionResolver")
|
|
|
|
|
// @Autowired
|
|
|
|
|
// private HandlerExceptionResolver resolver;
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException, IOException {
|
|
|
|
|
// HttpServletRequest req = (HttpServletRequest) servletRequest;
|
|
|
|
|
// String servletPath = req.getServletPath();
|
|
|
|
|
// HttpServletResponse rep = (HttpServletResponse) servletResponse;
|
|
|
|
|
// MyRequestWrapper requestWrapper = new MyRequestWrapper(req);
|
|
|
|
|
// if (ObjectUtil.notEqual(servletPath,"/backendApi/login/doLogin")){
|
|
|
|
|
// // 防止流读取一次后就没有了, 所以需要将流继续写出去
|
|
|
|
|
//
|
|
|
|
|
// // 获取请求参数
|
|
|
|
|
// Map<String, Object> paramsMaps = new TreeMap<>();
|
|
|
|
|
// if ("POST".equals(req.getMethod().toUpperCase())) {
|
|
|
|
|
// String body = requestWrapper.getBody();
|
|
|
|
|
// paramsMaps = JSONObject.parseObject(body, TreeMap.class);
|
|
|
|
|
// } else {
|
|
|
|
|
// Map<String, String[]> parameterMap = requestWrapper.getParameterMap();
|
|
|
|
|
// Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
|
|
|
|
|
// for (Map.Entry<String, String[]> next : entries) {
|
|
|
|
|
// paramsMaps.put(next.getKey(), next.getValue()[0]);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // 校验SQL注入
|
|
|
|
|
// if (ObjectUtil.isNotEmpty(paramsMaps)) {
|
|
|
|
|
// for (Object o : paramsMaps.entrySet()) {
|
|
|
|
|
// Map.Entry entry = (Map.Entry) o;
|
|
|
|
|
// Object value = entry.getValue();
|
|
|
|
|
// if (value != null) {
|
|
|
|
|
//
|
|
|
|
|
// boolean isValid = checkSqlInject(value.toString(), servletResponse);
|
|
|
|
|
// if (!isValid) {
|
|
|
|
|
// resolver.resolveException(req, rep,
|
|
|
|
|
// null, FileterException("客户端信息非法!!,存在特殊字符请重新输入"));
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// chain.doFilter(requestWrapper, servletResponse);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// //获取request请求body中参数
|
|
|
|
|
// public static String getBodyString(BufferedReader br) {
|
|
|
|
|
// String inputLine;
|
|
|
|
|
// String str = "";
|
|
|
|
|
// try {
|
|
|
|
|
// while ((inputLine = br.readLine()) != null) {
|
|
|
|
|
// str += inputLine;
|
|
|
|
|
// }
|
|
|
|
|
// br.close();
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
// System.out.println("IOException: " + e);
|
|
|
|
|
// }
|
|
|
|
|
// return str;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// /**
|
|
|
|
|
// * 检查SQL注入
|
|
|
|
|
// *
|
|
|
|
|
// * @param value 参数值
|
|
|
|
|
// * @param servletResponse 相应实例
|
|
|
|
|
// * @throws IOException IO异常
|
|
|
|
|
// */
|
|
|
|
|
// private boolean checkSqlInject(String value, ServletResponse servletResponse) throws IOException {
|
|
|
|
|
// if (null != value) {
|
|
|
|
|
// String output = value.replaceAll(SQL_REGX, "");
|
|
|
|
|
// if (output.length() < value.length()) {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// public Exception FileterException(String cause) {
|
|
|
|
|
// return new Exception(cause);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//}
|
|
|
|
|