content: package cn.org.alan.exam.config;
  
  import org.springframework.boot.web.servlet.FilterRegistrationBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.cors.CorsConfiguration;
  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  import org.springframework.web.filter.CorsFilter;
  
  import java.util.Collections;
  
  /**
   * CORS 资源共享配置
   *
   * @author Alan
   * @since 2023/4/17
   */
  @Configuration
  public class CorsConfig {
  
      /**
       * 配置允许跨域
       *
       * @return
       */
      @Bean
      public CorsFilter corsFilter() {
          CorsConfiguration config = new CorsConfiguration();
          config.addAllowedOrigin("*");
          config.setAllowCredentials(true);
          config.addAllowedMethod("OPTIONS");
          config.addAllowedMethod("HEAD");
          config.addAllowedMethod("GET");
          config.addAllowedMethod("PUT");
          config.addAllowedMethod("POST");
          config.addAllowedMethod("DELETE");
          config.addAllowedMethod("PATCH");
          config.addAllowedHeader("*");
          UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
          configurationSource.registerCorsConfiguration("/**", config);
          return new CorsFilter(configurationSource);
      }
  }