1、继承Filter的过滤类
package fly.cloud.bank.config.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @ProjectName: fly-cloud-bank
* @Package: fly.cloud.bank.config.filter
* @ClassName: BankParamFilter
* @Author: hfq
* @Description: bank服务入参特殊符号去除过滤器
* @Date: 2021/4/8 10:00
* @Version: 1.0
*/
@Component
public class BankParamFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("bank服务入参特殊符号去除过滤器");
ParameterRequestWrapper parameterRequestWrapper = new ParameterRequestWrapper((HttpServletRequest) servletRequest);
filterChain.doFilter(parameterRequestWrapper,servletResponse);
}
}
package fly.cloud.bank.config.filter;
import com.alibaba.fastjson.JSON;
import com.alipay.api.internal.util.file.IOUtils;
import com.aliyun.openservices.log.http.utils.HttpHeaders;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import fly.cloud.bank.urtils.CharacterUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;
/**
* @ProjectName: fly-cloud-bank
* @Package: fly.cloud.bank.config.filter
* @ClassName: ParameterRequestWrapper
* @Author: hfq
* @Description: 重写HttpServletRequestWrapper类用于修改参数
* @Date: 2021/4/8 10:20
* @Version: 1.0
*/
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
public ParameterRequestWrapper(HttpServletRequest request) {
super(request);
//只处理Post、PUT请求,这儿不用处理
}
/**
* 重写getInputStream方法 post类型的请求参数必须通过流才能获取到值
*/
@Override
public ServletInputStream getInputStream() throws IOException {
Map<String,Object> params = new HashMap<>();
if(!(super.getHeader(HttpHeaders.CONTENT_TYPE).equalsIgnoreCase(MediaType.APPLICATION_JSON_UTF8_VALUE) || super.getHeader(HttpHeaders.CONTENT_TYPE).equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE))){
return super.getInputStream();
}
//为空,直接返回
String json = IOUtils.toString(super.getInputStream(), "utf-8");
if (StringUtils.isEmpty(json)) {
return super.getInputStream();
}
System.out.println("转化前参数:"+json);
//reqBodyStr转为Map对象
params = new ObjectMapper().readValue(json, new TypeReference<HashMap<String, Object>>() {});
//去除特殊字符
modifyParameterValues(params);
System.out.println("转化后参数:"+JSON.toJSONString(params));
ByteArrayInputStream bis = new ByteArrayInputStream(JSON.toJSONString(params).getBytes("utf-8"));
return new MyServletInputStream(bis);
}
/**
* 将parameter的值去除特殊字符后重写回去
*/
public void modifyParameterValues(Map<String,Object> params){
Set<String> set = params.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext()){
String key= it.next();
//处理参数
getValueByKey(params,key);
}
}
private void getValueByKey(Map<String,Object> params,String key) {
Object value = judgeObject(params.get(key));
params.put(key,value);
}
private Object recursiveReplaceCharacter(Map<String, Object> vo) {
Map<String, Object> replaceMap = new HashMap<>();
for(String key : vo.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
Object replaceMapValue = vo.get(key);
Object a = judgeObject(replaceMapValue);
replaceMap.put(key,a);
}
return replaceMap;
}
private Object judgeObject(Object replaceMapValue) {
if (null == replaceMapValue) {
//key对应的值为空
return null;
}
if (replaceMapValue instanceof String) {
//key对应的值为String类型, 去空格后重新放入map
return CharacterUtils.replaceBankParameter((String) replaceMapValue);
} else if (replaceMapValue instanceof Map) {
return recursiveReplaceCharacter((Map<String,Object>) replaceMapValue);
} else if (replaceMapValue instanceof List) {
//key对应的值为List类型
List<Object> alist = (List<Object>) replaceMapValue;
for (int i = 0; i < alist.size(); i++) {
//遍历list
Object vol = alist.get(i);
if (vol instanceof String) {
//list里的元素为String, 去空格
alist.set(i,CharacterUtils.replaceBankParameter((String) vol));
} else if (vol instanceof Map) {
//list里的元素为Map, 递归处理
alist.set(i, recursiveReplaceCharacter((Map<String,Object>) vol));
}
}
return alist;
}
return replaceMapValue;
}
}
class MyServletInputStream extends ServletInputStream{
private ByteArrayInputStream bis;
public MyServletInputStream(ByteArrayInputStream bis){
this.bis=bis;
}
@Override
public boolean isFinished() {
return true;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener listener) {
}
@Override
public int read(){
return bis.read();
}
}
@Slf4j
public class CharacterUtils {
/**
* 替换银行入参特殊字符
*/
public static String replaceBankParameter(String str){
String regEx = "[‘&;\\[\\]<>|]";
str = Pattern.compile(regEx).matcher(str).replaceAll("").trim();
return str;
}
}