解决 springweb Filter 读取request body miss body
作者:互联网
package com.lb.demo.listener; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /××××
×
×
×
×添加此类
×
×
×
×××××××/ public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper { private final byte[] body; // 用于保存读取body中数据 public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); StringBuffer sb=new StringBuffer(); String temp=null; BufferedReader bufferread = request.getReader(); while((temp=bufferread.readLine())!=null) { sb.append(temp); } body = sb.toString().getBytes(); bufferread.close(); } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream())); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream bais = new ByteArrayInputStream(body); return new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } @Override public boolean isFinished() { // TODO Auto-generated method stub return false; } @Override public boolean isReady() { // TODO Auto-generated method stub return false; } @Override public void setReadListener(ReadListener arg0) { // TODO Auto-generated method stub } }; } }
package com.lb.demo.listener; import java.io.BufferedReader; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpFilter; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Component; /××××
×
×
×
×修改 Filter
×
×
××××××××××/
@Component public class MyServletFilter extends HttpFilter { String param = ""; private Log log = LogFactory.getLog(MyServletFilter.class); public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletRequest requestWrapper = null; requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request); // 替换 param = this.getBodyString(requestWrapper.getReader()); log.info("filter读取body中的参数>>>>>>>>>" + param); chain.doFilter(requestWrapper, response); } 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; } }
标签:body,springweb,request,javax,IOException,import,servlet,public 来源: https://www.cnblogs.com/libing029/p/11210766.html