content: package com.ld.poetry.config;
  
  import com.alibaba.fastjson.JSON;
  import com.ld.poetry.enums.CodeMsg;
  import com.ld.poetry.utils.CommonQuery;
  import com.ld.poetry.utils.PoetryUtil;
  import com.ld.poetry.utils.storage.FileFilter;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  @Component
  public class PoetryFilter extends OncePerRequestFilter {
  
      @Autowired
      private CommonQuery commonQuery;
  
      @Autowired
      private FileFilter fileFilter;
  
      @Override
      protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
          if (!"OPTIONS".equals(httpServletRequest.getMethod())) {
              try {
                  commonQuery.saveHistory(PoetryUtil.getIpAddr(httpServletRequest));
              } catch (Exception e) {
              }
  
              if (fileFilter.doFilterFile(httpServletRequest, httpServletResponse)) {
                  httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
                  httpServletResponse.setContentType("application/json;charset=UTF-8");
                  httpServletResponse.getWriter().write(JSON.toJSONString(PoetryResult.fail(CodeMsg.PARAMETER_ERROR.getCode(), CodeMsg.PARAMETER_ERROR.getMsg())));
                  return;
              }
          }
  
          filterChain.doFilter(httpServletRequest, httpServletResponse);
      }
  }
  