content: /*
   * 爱组搭，低代码组件化开发平台
   * ------------------------------------------
   * 受知识产权保护，请勿删除版权申明，开发平台不允许做非法网站，后果自负
   */
  package com.aizuda.security.request;
  
  import org.springframework.util.StreamUtils;
  
  import jakarta.servlet.ReadListener;
  import jakarta.servlet.ServletInputStream;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletRequestWrapper;
  import java.io.*;
  
  /**
   * 自定义 HttpServletRequestWrapper 来包装输入流
   * <p>
   * 尊重知识产权，CV 请保留版权，开发平台不允许做非法网站，后果自负
   *
   * @author imantou 青苗
   * @since 1.1.0
   */
  public class ParamsSignRequestWrapper extends HttpServletRequestWrapper {
  
      /**
       * 缓存下来的HTTP body
       */
      private byte[] body;
  
      public ParamsSignRequestWrapper(HttpServletRequest request) throws IOException {
          super(request);
          body = StreamUtils.copyToByteArray(request.getInputStream());
      }
  
      /**
       * 重新包装输入流
       * @return
       * @throws IOException
       */
      @Override
      public ServletInputStream getInputStream() throws IOException {
          final InputStream bodyStream = new ByteArrayInputStream(body);
          return new ServletInputStream() {
  
              @Override
              public int read() throws IOException {
                  return bodyStream.read();
              }
  
              @Override
              public boolean isFinished() {
                  return false;
              }
  
              @Override
              public boolean isReady() {
                  return true;
              }
  
              @Override
              public void setReadListener(ReadListener readListener) {
  
              }
          };
      }
      
      @Override
      public BufferedReader getReader() throws IOException {
          return new BufferedReader(new InputStreamReader(getInputStream()));
      }
  }