SpringBoot过滤器获取响应的参数

一、背景

在项目开发过程中,需要对于某些接口统一处理。
这时候就需要获取响应的报文,再对获取的报文进行统一处理。

二、了解过滤器

首先了解一下过滤器拦截器的区别:
JAVA中的拦截器、过滤器:https://blog.csdn.net/qq_38254635/article/details/131201519

三、代码

新建 WebResponseWrapper.java
重写 HttpServletResponseWrapper,处理json报文请求

package com.config.filter;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

public class WebResponseWrapper extends HttpServletResponseWrapper {
    private ByteArrayOutputStream buffer = null;
    private ServletOutputStream out = null;
    private PrintWriter writer = null;

    public WebResponseWrapper(HttpServletResponse response) throws IOException {
        super(response);
        buffer = new ByteArrayOutputStream();
        out = new WrapperOutputStream(buffer);
        writer = new PrintWriter(new OutputStreamWriter(buffer,this.getCharacterEncoding()));
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return out;
    }

    @Override
    public PrintWriter getWriter() throws UnsupportedEncodingException {
        return writer;
    }

    @Override
    public void flushBuffer() throws IOException {
        if (out != null) {
            out.flush();
        }
        if (writer != null) {
            writer.flush();
        }
    }

    @Override
    public void reset() {
        buffer.reset();
    }

    public byte[] getResponseData() throws IOException {
        flushBuffer();
        return buffer.toByteArray();
    }

    private class WrapperOutputStream extends ServletOutputStream {
        private ByteArrayOutputStream bos = null;

        public WrapperOutputStream(ByteArrayOutputStream stream)
                throws IOException {
            bos = stream;
        }

        @Override
        public void write(int b) throws IOException {
            bos.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            bos.write(b, 0, b.length);
        }

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

        @Override
        public void setWriteListener(WriteListener writeListener) {

        }
    }

}

新建WebResponseFilter.java,响应过滤器处理参数,加密、字符串替、添加响应参数等等。

package com.config.filter;

import com.Utils;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class WebResponseFilter implements Filter {

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

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        if(checkPath(httpRequest.getRequestURI())){
            chain.doFilter(httpRequest, httpResponse);
        } else {
            response.setContentType("application/json; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            WebResponseWrapper webResponseWrapper = new WebResponseWrapper((HttpServletResponse) response);
            chain.doFilter(request, webResponseWrapper);
            byte[] resData = webResponseWrapper.getResponseData();
            PrintWriter out = response.getWriter();
			String encrypt = Utils.handleResponse(new String(resData, "UTF-8"));
			out.print(encrypt.replaceAll("[\\s*\t\n\r]", ""));
            out.flush();
            out.close();
        }
    }

    private boolean checkPath(String url){
        if(!"on".equals(ENCRYPT_SWITCH)) return true;
        for (String path : RESPONSE_LIST) {
            if(url.contains(path)) return true;
        }
        return false;
    }

    @Override
    public void destroy() {

    }

}

说明:
ENCRYPT_SWITCH:开关配置
RESPONSE_LIST:忽略处理路径配置,如一些导出的操作。
Utils.handleResponse():需要对报文进行的处理操作。

相关链接:
SpringBoot过滤器获取请求的参数:https://blog.csdn.net/qq_38254635/article/details/136041159
JAVA中的拦截器、过滤器:https://blog.csdn.net/qq_38254635/article/details/131201519

OK,就这些吧。

有什么不对的还望指正,书写不易,觉得有帮助就点个赞吧!☺☺☺

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

message丶小和尚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值