通过 filter 过滤器对请求参数进行处理并修改
问题描述:
通过过滤器,对前端请求过来的参数进行处理(本次是对请求参数进行前后空格的去除),然后用处理后的参数进行数据查询。
解决方案
1、重写 HttpServletRequestWrapper 类
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
// 存放JSON数据主体
private String body;
public ParameterRequestWrapper(HttpServletRequest request, String context) {
super(request);
body = context;
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes("UTF-8"));
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
2、新增过滤器
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.common.filter.ParameterRequestWrapper;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Set;
/**
* 传输过滤器,去除传参的前后空格
*/
@Component
public class TrimEntityFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
try {
// 从请求中获取参数信息
JSONObject body = getBody(request);
// 去除参数中字段的前后空格
String newBody = trimParams(body);
if (StrUtil.isNotBlank(newBody)) {
//定义一个新的request
ParameterRequestWrapper wrapper = new ParameterRequestWrapper(request, newBody);
chain.doFilter(wrapper, response);
} else {
chain.doFilter(request, response);
}
} catch (Exception e) {
chain.doFilter(request, response);
}
}
/**
* 从请求中获取参数值
* @param request 请求体
* @return 获取到的参数值
*/
private JSONObject getBody(HttpServletRequest request) throws IOException {
StringBuilder data = new StringBuilder();
String line;
BufferedReader reader = request.getReader();
while (null != (line = reader.readLine())) {
data.append(line);
}
// 打印获取到的请求参数值
return JSONObject.parseObject(data.toString());
}
/**
* 将获取到的参数信息去除前后空格
* @param paramJson body 参数值
* @return 去前后空格后的参数串
*/
private String trimParams(JSONObject paramJson) {
// 判断值是不是字符串,是的话去除前后空格
Set<String> keySet = paramJson.keySet();
for (String key : keySet) {
Object value = paramJson.get(key);
if (value instanceof String) {
paramJson.put(key, ((String) value).trim());
}
}
return paramJson.toJSONString();
}
}