package com.bonus.common.security.config;


import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.global.SystemGlobal;
import com.bonus.common.security.interceptor.ReadHttpRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 *
 * @author bonus
 */
@Component
@WebFilter("/*")
@Slf4j
public class MyFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        log.debug("进入MyFilter过滤器,parameterMap");
        if (SystemGlobal.POST.equals(request.getMethod()) ||   SystemGlobal.PUT.equals(request.getMethod()) ){
          //去除文件上传
             String header = request.getHeader(HttpHeaders.CONTENT_TYPE);
             if(StringUtils.isNotEmpty(header)){
                    if(header.contains(MediaType.MULTIPART_FORM_DATA_VALUE)){
                        filterChain.doFilter(request, response);
                        return;
                    }
             }
            //解决 无法流重复读取的问题
            ReadHttpRequestWrapper requestWrapper = new ReadHttpRequestWrapper(request, request.getParameterMap());
            filterChain.doFilter(requestWrapper, response);
      }else{
          filterChain.doFilter(request, response);
      }
      }

}