SpringBoot过滤器获取响应的参数

一、背景

在项目开发过程中,需要对于某些接口统一处理。

这时候就需要获取响应的报文,再对获取的报文进行统一处理。

二、了解过滤器

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

三、代码

新建 WebResponseWrapper.java

重写 HttpServletResponseWrapper,处理json报文请求

java 复制代码
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,响应过滤器处理参数,加密、字符串替、添加响应参数等等。

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,就这些吧。

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

相关推荐
monkey_meng5 分钟前
【Rust类型驱动开发 Type Driven Development】
开发语言·后端·rust
落落落sss13 分钟前
MQ集群
java·服务器·开发语言·后端·elasticsearch·adb·ruby
我救我自己13 分钟前
UE5运行时创建slate窗口
java·服务器·ue5
2401_8532757334 分钟前
ArrayList 源码分析
java·开发语言
爪哇学长38 分钟前
SQL 注入详解:原理、危害与防范措施
xml·java·数据库·sql·oracle
大鲤余43 分钟前
Rust,删除cargo安装的可执行文件
开发语言·后端·rust
她说彩礼65万1 小时前
Asp.NET Core Mvc中一个视图怎么设置多个强数据类型
后端·asp.net·mvc
MoFe11 小时前
【.net core】【sqlsugar】字符串拼接+内容去重
java·开发语言·.netcore
陈随易1 小时前
农村程序员-关于小孩教育的思考
前端·后端·程序员
_江南一点雨1 小时前
SpringBoot 3.3.5 试用CRaC,启动速度提升3到10倍
java·spring boot·后端