Java小强个人技术博客站点    手机版
当前位置: 首页 >> 框架 >> SpringBoot项目中拦截器获取Body参数的问题

SpringBoot项目中拦截器获取Body参数的问题

91580 框架 | 2022-6-30

首先我们要知道一个问题:

HttpServletRequest的输入流只能读取一次,如果你在拦截器读取了Body的参数,那么在Controller再次读取时,会直接报错,原因如下:

我们先来看看为什么HttpServletRequest的输入流只能读一次,当我们调用getInputStream()方法获取输入流时得到的是一个InputStream对象,而实际类型是ServletInputStream,它继承于InputStream。

InputStream的read()方法内部有一个postion,标志当前流被读取到的位置,每读取一次,该标志就会移动一次,如果读到最后,read()会返回-1,表示已经读取完了。如果想要重新读取则需要调用reset()方法,position就会移动到上次调用mark的位置,mark默认是0,所以就能从头再读了。调用reset()方法的前提是已经重写了reset()方法,当然能否reset也是有条件的,它取决于markSupported()方法是否返回true。

拦截器.jpg


InputStream默认不实现reset(),并且markSupported()默认也是返回false。

/**
 * Repositions this stream to the position at the time the
 * <code>mark</code> method was last called on this input stream.
 *
 * <p> The general contract of <code>reset</code> is:
 *
 * <ul>
 * <li> If the method <code>markSupported</code> returns
 * <code>true</code>, then:
 *
 *     <ul><li> If the method <code>mark</code> has not been called since
 *     the stream was created, or the number of bytes read from the stream
 *     since <code>mark</code> was last called is larger than the argument
 *     to <code>mark</code> at that last call, then an
 *     <code>IOException</code> might be thrown.
 *
 *     <li> If such an <code>IOException</code> is not thrown, then the
 *     stream is reset to a state such that all the bytes read since the
 *     most recent call to <code>mark</code> (or since the start of the
 *     file, if <code>mark</code> has not been called) will be resupplied
 *     to subsequent callers of the <code>read</code> method, followed by
 *     any bytes that otherwise would have been the next input data as of
 *     the time of the call to <code>reset</code>. </ul>
 *
 * <li> If the method <code>markSupported</code> returns
 * <code>false</code>, then:
 *
 *     <ul><li> The call to <code>reset</code> may throw an
 *     <code>IOException</code>.
 *
 *     <li> If an <code>IOException</code> is not thrown, then the stream
 *     is reset to a fixed state that depends on the particular type of the
 *     input stream and how it was created. The bytes that will be supplied
 *     to subsequent callers of the <code>read</code> method depend on the
 *     particular type of the input stream. </ul></ul>
 *
 * <p>The method <code>reset</code> for class <code>InputStream</code>
 * does nothing except throw an <code>IOException</code>.
 *
 * @exception  IOException  if this stream has not been marked or if the
 *               mark has been invalidated.
 * @see     java.io.InputStream#mark(int)
 * @see     java.io.IOException
 */
public synchronized void reset() throws IOException {
    throw new IOException("mark/reset not supported");
}

我们再来看看ServletInputStream,可以看到该类没有重写mark(),reset()以及markSupported()方法:

ServletInputStream.jpg

综上,InputStream默认不实现reset的相关方法,而ServletInputStream也没有重写reset的相关方法,这样就无法重复读取流,这就是我们从request对象中获取的输入流就只能读取一次的原因。


解决方案,使用HttpServletRequestWrapper + Filter解决输入流不能重复读取问题。

既然ServletInputStream不支持重新读写,那么为什么不把流读出来后用容器存储起来,后面就可以多次利用了。那么问题就来了,要如何存储这个流呢?

所幸JavaEE提供了一个HttpServletRequestWrapper类,从类名也可以知道它是一个http请求包装器,其基于装饰者模式实现了HttpServletRequest界面。该类并没有真正去实现HttpServletRequest的方法,而只是在方法内又去调用HttpServletRequest的方法,所以我们可以通过继承该类并实现想要重新定义的方法以达到包装原生HttpServletRequest对象的目的。


首先我们要定义一个容器,将输入流里面的数据存储到这个容器里,这个容器可以是数组或集合。然后我们重写getInputStream方法,每次都从这个容器里读数据,这样我们的输入流就可以读取任意次了。

package com.example.springboot.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.nio.charset.Charset;

/**
 * 包装HttpServletRequest,目的是让其输入流可重复读
 **/
@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {
    /**
     * 存储body数据的容器
     */
    private final byte[] body;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        // 将body数据存储起来
        String bodyStr = getBodyString(request);
        body = bodyStr.getBytes(Charset.defaultCharset());
    }

    /**
     * 获取请求Body
     */
    public String getBodyString(final ServletRequest request) {
        try {
            return inputStream2String(request.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取请求Body
     */
    public String getBodyString() {
        final InputStream inputStream = new ByteArrayInputStream(body);
        return inputStream2String(inputStream);
    }

    /**
     * 将inputStream里的数据读取出来并转换成字符串
     */
    private String inputStream2String(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("", e);
                }
            }
        }

        return sb.toString();
    }

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

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(body);
        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return inputStream.read();
            }

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

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

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }
}

除了要写一个包装器外,我们还需要在过滤器里将原生的HttpServletRequest对象替换成我们的RequestWrapper对象,代码如下:

package com.example.springboot.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 替换HttpServletRequest
 **/
@Slf4j
public class ReplaceStreamFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = new RequestWrapper((HttpServletRequest) request);
        chain.doFilter(requestWrapper, response);
    }

    @Override
    public void destroy() {
    }
}

然后我们就可以在拦截器中获取Body数据也不慌Controller层会报错了:

package com.example.springboot.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 编写拦截器
 **/
@Slf4j
public class SignatureInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (isJson(request)) {
            // 获取json字符串
            String jsonParam = new RequestWrapper(request).getBodyString();
            System.out.println("SignatureInterceptor:" + jsonParam);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    /**
     * 判断本次请求的数据类型是否为json
     */
    private boolean isJson(HttpServletRequest request) {
        if (request.getContentType() != null) {
            return request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE) ||
                    request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE);
        }
        return false;
    }
}

过滤器和拦截器在配置类中进行注册才会生效,过滤器配置类代码如下:

package com.example.springboot.config;

import com.example.springboot.filter.ReplaceStreamFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * 过滤器配置类
 **/
@Configuration
public class FilterConfig {

    /**
     * 注册过滤器
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(replaceStreamFilter());
        registration.addUrlPatterns("/*");
        registration.setName("streamFilter");
        return registration;
    }

    /**
     * 实例化StreamFilter
     */
    @Bean(name = "replaceStreamFilter")
    public Filter replaceStreamFilter() {
        return new ReplaceStreamFilter();
    }
}

注册拦截器

package com.example.springboot.config;

import com.example.springboot.filter.SignatureInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 注册拦截器
 **/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public SignatureInterceptor getSignatureInterceptor() {
        return new SignatureInterceptor();
    }

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getSignatureInterceptor())
                .addPathPatterns("/**");
    }
}


编写测试Controller

package com.example.springboot.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 测试XSS
 */
@RestController
@RequestMapping("/test")
public class XssController {
    @GetMapping("/xss")
    public String xssGet(@RequestParam("QueryData") String QueryData, @RequestBody Map<String,Object> body, HttpServletRequest request, HttpServletResponse response){
        System.out.println(QueryData);
        System.out.println(request.getHeader("HeaderData"));
        System.out.println(body);
        return QueryData;
    }
}


发送测试请求

api Post.jpg


注意,在实践中发现,在判断是否为JSON数据时

public static final String APPLICATION_JSON_VALUE = "application/json";
public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

有时变量中间是带空格的,因为上述代码中的判断方法改为:

/**
 * 判断本次请求的数据类型是否为json
 */
private boolean isJson(HttpServletRequest request) {
	if (request.getContentType() != null) {
		return request.getContentType().contains("application/json");
	}
	return false;
}


END

推荐您阅读更多有关于“ json springboot 拦截器 body 过滤器 HttpServletRequest ”的文章

上一篇:永远跟着党走|庆祝中国共产党成立101周年 下一篇:使用Redis实现用户最近浏览记录

猜你喜欢

发表评论: