Spring/SpringBoot 过滤器/拦截器 获取http 请求request中的参数 和 response返回值

需求:每个接口被调用时,获取接口的请求参数及返回值,进行保存

参考:
https://www.jianshu.com/p/69c6fba08c92
https://blog.csdn.net/qq_33206732/article/details/78623042
思考:
1.request 和 response 中的数据都是 存在流中的(缓存中)获取一次就没有了,需要重新写回去。
2.通过拦截器 获取,但是拦截器中获取不到 response 的返回值
3.通过 实现 RequestBodyAdvice和ResponseBodyAdvice两个接口 进行操作,很方便,但是我不会。。。我觉得 这是两个类,很难同时保存一个请求中的请求参数和返回值
4.最后决定用 filter 实现。
首先需要 继承HttpServletRequestWrapper 和 HttpServletResponseWrapper 两个包装类对 request和response进行包装,从中获取数据,代码整理如下。
(我是新人,不知道怎么写,直接看代码吧 -.-!)

package com.hoau.psb.servlet;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

public class RequestWrapper extends HttpServletRequestWrapper {
    private final String body;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }
        body = stringBuilder.toString();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            public boolean isFinished() {
                return false;
            }

            public boolean isReady() {
                return false;
            }

            public void setReadListener(ReadListener readListener) {
            }

            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
        return servletInputStream;

    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() {
        return this.body;
    }

}


package com.hoau.psb.servlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;

/**
 * @param
 * @author 
 * @create 2019-06-10 13:31
 * @return
 * @description :
 */
public class ResponseWrapper extends HttpServletResponseWrapper {

    private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    private HttpServletResponse response;
    private PrintWriter pwrite;

    public ResponseWrapper(HttpServletResponse response) {
        super(response);
        this.response = response;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        // 将数据写到 byte 中
        return new MyServletOutputStream(bytes);
    }

    /**
     * 重写父类的 getWriter() 方法,将响应数据缓存在 PrintWriter 中
     */
    @Override
    public PrintWriter getWriter() throws IOException {
        try {
            pwrite = new PrintWriter(new OutputStreamWriter(bytes, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pwrite;
    }

    /**
     * 获取缓存在 PrintWriter 中的响应数据
     *
     * @return
     */
    public byte[] getBytes() {
        if (null != pwrite) {
            pwrite.close();
            return bytes.toByteArray();
        }

        if (null != bytes) {
            try {
                bytes.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes.toByteArray();
    }

    class MyServletOutputStream extends ServletOutputStream {
        private ByteArrayOutputStream ostream;

        public MyServletOutputStream(ByteArrayOutputStream ostream) {
            this.ostream = ostream;
        }

        @Override
        public void write(int b) throws IOException {
            ostream.write(b); // 将数据写到 stream 中
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setWriteListener(WriteListener listener) {

        }
    }

}
package com.hoau.psb.filter;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.hoau.psb.entity.MonitorData;
import com.hoau.psb.facade.IhoauMonitorDataFacade;
import com.hoau.psb.servlet.RequestWrapper;
import com.hoau.psb.servlet.ResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/psb/*", filterName = "monitorDataFilter")
public class MonitorDataFilter implements Filter {


    @Reference
    IhoauMonitorDataFacade ihoauMonitorDataServiceImpl;

    Log log = LogFactory.getLog(MonitorDataFilter.class);

    private MonitorData monitorData;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
        ResponseWrapper responseWrapper = new ResponseWrapper(httpServletResponse);

        filterChain.doFilter(requestWrapper, responseWrapper);

        monitorData = new MonitorData();
        String params = null;
        String val = null;

        String meth = httpServletRequest.getMethod();

        //delete 和put 请求暂时未处理
        if ("POST".equals(meth)) {
            params = requestWrapper.getBody();
        } else {
            params = JSONObject.toJSONString(httpServletRequest.getParameterMap());
        }
        log.info(params);

        monitorData.setInterAddress(httpServletRequest.getRequestURL().toString());
        monitorData.setAccAccount("");
        monitorData.setAccIP(httpServletRequest.getRemoteAddr());
        monitorData.setAccParams(params);
        monitorData.setInterType("");

        byte[] bytes = responseWrapper.getBytes();
        val = new String(bytes, "UTF-8");
        monitorData.setRetVal(JSONObject.toJSONString(val));

        log.info(val);

        //将数据 再写到 response 中
        servletResponse.getOutputStream().write(bytes);
        servletResponse.getOutputStream().flush();
        servletResponse.getOutputStream().close();

        ihoauMonitorDataServiceImpl.addMonitorData(monitorData);

    }

    @Override
    public void destroy() {

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值