content: package io.github.xxyopen.novel.core.config;
  
  import lombok.RequiredArgsConstructor;
  import org.springframework.boot.context.properties.EnableConfigurationProperties;
  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;
  
  /**
   * 跨域配置
   *
   * @author xiongxiaoyang
   * @date 2022/5/13
   */
  @Configuration
  @EnableConfigurationProperties(CorsProperties.class)
  @RequiredArgsConstructor
  public class CorsConfig {
  
      private final CorsProperties corsProperties;
  
      @Bean
      public CorsFilter corsFilter() {
          CorsConfiguration config = new CorsConfiguration();
          // 允许的域,不要写*，否则cookie就无法使用了
          for (String allowOrigin : corsProperties.allowOrigins()) {
              config.addAllowedOrigin(allowOrigin);
          }
          // 允许的头信息
          config.addAllowedHeader("*");
          // 允许的请求方式
          config.addAllowedMethod("*");
          // 是否允许携带Cookie信息
          config.setAllowCredentials(true);
  
          UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
          // 添加映射路径，拦截一切请求
          configurationSource.registerCorsConfiguration("/**", config);
          return new CorsFilter(configurationSource);
      }
  
  }
  