content: package openjoe.smart.sso.demo;
  
  import jakarta.servlet.*;
  import jakarta.servlet.annotation.WebFilter;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.annotation.Order;
  
  import java.io.IOException;
  
  /**
   * 跨域过滤器
   * 注：跨域过滤器@Order设置的数值，必须要优先级高于ClientContainer设置的Order，详见ClientProperties.order属性
   *
   * @author Joe
   */
  @Order(5)
  @WebFilter
  @Configuration
  public class CorsFilter implements Filter {
      @Override
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
          HttpServletRequest request = (HttpServletRequest) req;
          HttpServletResponse response = (HttpServletResponse) res;
          // 允许指定域访问跨域资源
          response.setHeader("Access-Control-Allow-Origin", "*");
          // 允许所有请求方式
          response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
          // 有效时间
          response.setHeader("Access-Control-Max-Age", "3600");
          // 允许的header参数
          response.setHeader("Access-Control-Allow-Headers", "*");
          // 如果是预检请求，直接返回
          if ("OPTIONS".equals(request.getMethod())) {
              response.getWriter().print("");
              return;
          }
          chain.doFilter(req, res);
      }
  }