• Java 后台多次获取requestBody


    如今越来越多的项目基于Springboot进行开发,在项目中拦截器的使用是不可缺少的,因为需要对body中的值进行校验,例如加密验签、防重复提交、内容校验等等,对于可以多次读取body的解决方案如下:

    1 新建HttpRequestWrapperUtil

    public class HttpRequestWrapperUtil {
    public static String getBodyString(HttpServletRequest request) throws IOException {
    StringBuilder sb = new StringBuilder();
    InputStream inputStream = null;
    BufferedReader reader = null;
    try {
    inputStream = request.getInputStream();
    reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
    String line = "";
    while ((line = reader.readLine()) != null) {
    sb.append(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (inputStream != null) {
    try {
    inputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    return sb.toString();
    }
    }

    2 新建RequestReaderHttpServletRequestWrapper

    public class RequestReaderHttpServletRequestWrapper extends HttpServletRequestWrapper{

    private final byte[] body;

    public RequestReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
    super(request);
    body = HttpRequestWrapperUtil.getBodyString(request).getBytes(Charset.forName("UTF-8"));
    }

    @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 readListener) {

    }
    };
    }
    }


    3 新建HttpServletRequestReplacedFilter(过滤器)

    public class HttpServletRequestReplacedFilter implements Filter {
    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    ServletRequest requestWrapper = null;
    if(request instanceof HttpServletRequest) {
    requestWrapper = new RequestReaderHttpServletRequestWrapper((HttpServletRequest) request);
    }
    //获取请求中的流如何,将取出来的字符串,再次转换成流,然后把它放入到新request对象中。
    // 在chain.doFiler方法中传递新的request对象
    if(requestWrapper == null) {
    chain.doFilter(request, response);
    } else {
    chain.doFilter(requestWrapper, response);
    }
    }

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

    }
    }

    这样就可以重复读取body中的内容了
  • 相关阅读:
    关于linux命令 cp,rm,mv执行时是否询问的问题,不同用户有不同的别名设置
    用jquery根据json动态创建多级菜单导航(by https://www.cnblogs.com/fatty-yu/p/7088955.html)
    前台向后台传值的两种方法 以及 从后台获取数据的方法
    “加载更多”功能如何实现
    AngularJS的主要组成部分:
    数组与字符串的相互转化
    angular过滤器的使用 By http://www.tuicool.com/articles/ueUZBv
    transclude
    内嵌模版 template 原创
    导航栏选中效果 ng-repeat
  • 原文地址:https://www.cnblogs.com/jinlongyu123/p/10109633.html
Copyright © 2020-2023  润新知