欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决request.getInputStream()只能读取一次的方案

程序员文章站 2022-03-10 12:10:00
...

 最近做的一个项目中,需要在拦截器中校验参数是否被篡改,所以需要在json字符串反序列化前,提前获取request中的字节数据,但这样会在后续反序列化时报错,原因是request.getInputStream()只能被调用一次,解决办法就是在过滤器中对request请求进行包装,创建一个body[]数组,将流中数据读出保存到body中,以便后续可以反复读取,在重写getInputStream()方法,下附解决步骤:

1.创建request请求包装类的对象

//request请求的包装类
public class RequestWrapper extends HttpServletRequestWrapper {
    String default_charset = "utf-8";

    private final byte[] body;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        ServletInputStream inputStream = request.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len=0;
        while ((len = inputStream.read(buf))!=-1){
            out.write(buf,0,len);
        }
        out.flush();
        this.body = out.toByteArray();
        out.close();
    }

    public byte[] getBody() {
        return body;
    }

    public String getBodyString() {
       return new String(body, Charset.forName(default_charset));
    }

    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(this.body);
        return new ServletInputStream() {
            public int read() throws IOException {
                return inputStream.read();
            }

            public boolean isFinished() {
                return false;
            }

            public boolean isReady() {
                return false;
            }
            public void setReadListener(ReadListener readListener) {}
        };
    }

}

2.在过滤器中进行用自己创建的包装类对象替换

public class ReplaceStreamFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        RequestWrapper wrapper = new RequestWrapper((HttpServletRequest)request);
        log.info("request请求对象被替换。。。。。。。。。。。。。。。。。。");
        chain.doFilter(wrapper,response);
    }
    @Override
    public void destroy() {
    }
}

3.在后续校验中直接获取即可