SpringBoot过滤器获取请求的参数

一、背景

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

二、了解过滤器

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

三、代码

新建 WebRequestWrapper.java
重写 HttpServletRequestWrapper,处理json报文请求

package com.config.filter;

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;
import org.springframework.util.StreamUtils;

public class WebRequestWrapper extends HttpServletRequestWrapper{

    private byte[] body;

    public WebRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = StreamUtils.copyToByteArray(request.getInputStream());
    }

    @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() {
                return false;
            }

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

            @Override
            public void setReadListener(ReadListener listener) {

            }
        };
    }

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

    public void setBody(byte[] body) {
        this.body = body;
    }

}

新建WebRequestFilter.java,请求过滤器处理参数,解密、字符串替、添加请求参数等等。

package com.config.filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
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 com.Utils;
import org.springframework.stereotype.Component;

@Component
public class WebRequestFilter implements Filter {

    @Override
    public void init(FilterConfig arg0) 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(request, response);
        } else {
            WebRequestWrapper requestWrapper = new WebRequestWrapper((HttpServletRequest) request);
            BufferedReader br = requestWrapper.getReader();
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String original = sb.toString();
            String handle = Utils.handleRequest(original);
            requestWrapper.setBody(handle.getBytes("UTF-8"));
            chain.doFilter(requestWrapper, response);
        }
    }
	
    private boolean checkPath(String url){
        if(!"on".equals(ENCRYPT_SWITCH)) return true;
        for (String path : REQUEST_LIST) {
            if(url.contains(path)) return true;
        }
        return false;
    }

    @Override
    public void destroy() {

    }

}

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

新建 WebRequestConfig.java 配置过滤器的执行顺序。
order属性控制过滤器加载顺序:数字越小,加载越早

package com.config.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebRequestConfig {
    
    @Bean
    public FilterRegistrationBean ValidatorFilterRegistration() {
        FilterRegistrationBean<WebRequestFilter> registration = new FilterRegistrationBean<WebRequestFilter>();
        registration.setFilter(new WebRequestFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(Integer.MAX_VALUE-10);
        return registration;
    }
}

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

OK,就这些吧。

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

参考链接:https://www.jb51.net/article/221072.html

你可能感兴趣的:(java,spring,boot,后端,java)