使用HttpServletRequestWrapper解决web项目request数据流无法重复读取的问题

在做web项目开发时,我们有时候需要做一些前置的拦截判断处理,比如非法参数校验,防攻击拦截,统一日志处理等,而请求参数如果是form表单提交还好处理;对于json这种输入流的数据就会有问题,统一处理如果读取了数据流就会将流进行关闭,这就会导致接下来的业务处理无法读取数据流。为了解决这个问题,需要将request中的输入流包装为可以重复读取的数据流,具体的操作如下:

自定义一个类继承HttpServletRequestWrapper,并实现它里面的相关方法:

java 复制代码
import cn.hutool.core.io.IoUtil;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author xingo
 * @Date 2024/1/26
 */
public class RepeatableReadRequestWrapper extends HttpServletRequestWrapper {

    private final byte[] body;

    public RepeatableReadRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        request.setCharacterEncoding("UTF-8");
        body = IoUtil.readBytes(request.getInputStream());
    }

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

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream bis = new ByteArrayInputStream(body);

        return new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return bis.read();
            }

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

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

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }

}

封装成这个类就是为了解决需要重复读取输入流的地方就使用这个包装类替换原有的request对象。再定义一个过滤器用于模拟统一处理请求参数,下面就简单模拟在参数中取用户名的过滤器:

java 复制代码
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * @Author xingo
 * @Date 2024/1/26
 */
@Order(value = Ordered.LOWEST_PRECEDENCE - 1)
@Component
@WebFilter(filterName = "paramsFilter", urlPatterns = "/*")
public class CheckParamsFilter implements Filter {

    private ServletContext context;
    static final String checkKey = "userName";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
        context = filterConfig.getServletContext();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        ServletRequest requestWrapper = null;
        String userName = null;
        String contentType = request.getContentType();
        if(contentType != null && contentType.contains(MediaType.APPLICATION_JSON_VALUE)) {
            try {
                // 对于需要读取输入流的先对request进行包装处理,这样后续再次需要读取数据流时就可以正常读到
                requestWrapper = new RepeatableReadRequestWrapper(request);

                JsonNode jsonNode = JacksonUtils.getObjectMapper().readTree(requestWrapper.getInputStream());
                if(jsonNode.get(checkKey) != null) {
                    userName = jsonNode.get(checkKey).asText();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                if(request.getParameter(checkKey) != null) {
                    userName = request.getParameter(checkKey);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(userName != null) {
            // 这里判断用户名检查成功就放行、否则就返回失败信息,在放行处理时需要判断是否需要传递包装request
            if(this.check(userName)) {
                chain.doFilter(Objects.requireNonNullElse(requestWrapper, servletRequest), servletResponse);
            }
            servletResponse.setContentType("application/json; charset=utf-8");
            servletResponse.getWriter().print(JacksonUtils.toJSONString(ApiResult.fail(400, "信息验证失败")));

            return;
        }

        chain.doFilter(Objects.requireNonNullElse(requestWrapper, servletRequest), servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    private boolean check(String userName) {
        return "admin".equals(userName);
    }
}
相关推荐
2401_8543910814 分钟前
城镇住房保障:SpringBoot系统功能概览
java·spring boot·后端
2401_857439691 小时前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
哎呦没5 小时前
SpringBoot框架下的资产管理自动化
java·spring boot·后端
2401_857600955 小时前
SpringBoot框架的企业资产管理自动化
spring boot·后端·自动化
NiNg_1_2349 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
种树人202408199 小时前
如何在 Spring Boot 中启用定时任务
spring boot
苹果醋312 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
Wx-bishekaifayuan12 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer0812 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源