通过 filter 过滤器对请求参数进行处理并修改
- 问题描述:
- 解决方案
- 1、重写 HttpServletRequestWrapper 类
- 2、新增过滤器
问题描述:
通过过滤器,对前端请求过来的参数进行处理(本次是对请求参数进行前后空格的去除),然后用处理后的参数进行数据查询。
解决方案
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;}@Overridepublic ServletInputStream getInputStream() throws IOException {final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes("UTF-8"));ServletInputStream servletInputStream = new ServletInputStream() {@Overridepublic int read() throws IOException {return byteArrayInputStream.read();}@Overridepublic boolean isFinished() {return false;}@Overridepublic boolean isReady() {return false;}@Overridepublic void setReadListener(ReadListener listener) {}};return servletInputStream;}@Overridepublic 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 {@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException {try {// 从请求中获取参数信息JSONObject body = getBody(request);// 去除参数中字段的前后空格String newBody = trimParams(body);if (StrUtil.isNotBlank(newBody)) {//定义一个新的requestParameterRequestWrapper 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();}
}