这篇文章主要为大家展示了“SpringBoot怎么使用filter对request body参数进行校验”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“springboot怎么使用fil
这篇文章主要为大家展示了“SpringBoot怎么使用filter对request body参数进行校验”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“springboot怎么使用filter对request body参数进行校验”这篇文章吧。
@Slf4jpublic class ParameterCheckServletRequestWrapper extends httpservletRequestWrapper { private byte[] requestBody; private Charset charSet; public ParameterCheckServletRequestWrapper(HttpServletRequest request) { super(request); //缓存请求body try { String requestBodyStr = getRequestPostStr(request); if (StringUtils.isNotBlank(requestBodyStr)) { JSONObject resultjson = JSONObject.fromObject(requestBodyStr.replace("\"", "'")); Object[] obj = resultJson.keySet().toArray(); for (Object o : obj) { resultJson.put(o, StringUtils.trimToNull(resultJson.get(o).toString())); } requestBody = resultJson.toString().getBytes(charSet); } else { requestBody = new byte[0]; } } catch (ioException e) { log.error("", e); } } public String getRequestPostStr(HttpServletRequest request) throws IOException { String charSetStr = request.getCharacterEncoding(); if (charSetStr == null) { charSetStr = "UTF-8"; } charSet = Charset.forName(charSetStr); return StreamUtils.copyToString(request.getInputStream(), charSet); } @Override public ServletInputStream getInputStream() { if (requestBody == null) { requestBody = new byte[0]; } final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody); return new ServletInputStream() { @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } @Override public int read() { return byteArrayInputStream.read(); } }; } @Override public BufferedReader getReader() { return new BufferedReader(new InputStreamReader(getInputStream())); }}
public class ParameterCheckFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { ParameterCheckServletRequestWrapper myWrapper = new ParameterCheckServletRequestWrapper((HttpServletRequest) servletRequest); filterChain.doFilter(myWrapper, servletResponse); } @Override public void destroy() { }}
@Configurationpublic class FilterConfig { @Bean public FilterReGIStrationBean authFilterRegistrationBean() { FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setName("parameterCheckFilter"); registrationBean.setFilter(new ParameterCheckFilter()); registrationBean.setOrder(1); registrationBean.addUrlPatterns("public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper { private String tempBody; public MyHttpServletRequestWrapper(HttpServletRequest request) { super(request); this.tempBody = getBody(request); System.out.println(tempBody); } private String getBody(HttpServletRequest request) { try { ServletInputStream stream = request.getInputStream(); String read = ""; StringBuilder stringBuilder = new StringBuilder(); byte[] b = new byte[1024]; int lens = -1; while ((lens = stream.read(b)) > 0) { stringBuilder.append(new String(b, 0, lens)); } return stringBuilder.toString(); } catch (IOException e) { throw new RuntimeException(e); } } public String getBody() { MonitorPointQueryPara para = JSON.parseObject(tempBody, MonitorPointQueryPara.class); para.setName("1232321321"); tempBody = JSONObject.toJSONString(para); return tempBody; } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream())); } @Override public ServletInputStream getInputStream() throws IOException { // 创建字节数组输入流 final ByteArrayInputStream bais = new ByteArrayInputStream(tempBody.getBytes(Charset.defaultCharset())); return new ServletInputStream() { @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } @Override public int read() throws IOException { return bais.read(); } }; }}
新建MyFilter 继承 Filter
添加@WEBFilter注解
启动类添加@ServletComponentScan(如下)
package com.orisdom.modules.common.filter;import org.springframework.core.annotation.Order;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.io.BufferedReader;import java.io.IOException;import java.util.HashMap;import java.util.Map;@WebFilterpublic class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { MyHttpServletRequestWrapper myHttpServletRequestWrapper = new MyHttpServletRequestWrapper((HttpServletRequest) servletRequest); // 相当于赋值 myHttpServletRequestWrapper.getBody(); // 自己定义的MyHttpServletRequestWrapper filterChain.doFilter(myHttpServletRequestWrapper, servletResponse); System.out.println(11111111); } @Override public void destroy() { }}
没加之前
加了之后
以上是“Springboot怎么使用filter对request body参数进行校验”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Springboot怎么使用filter对request body参数进行校验
本文链接: https://lsjlt.com/news/324790.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0