content: package com.domaindns.config;
  
  import org.springframework.beans.factory.annotation.Value;
  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.Arrays;
  
  @Configuration
  public class CorsConfig {
      @Value("${cors.allow-origins:*}")
      private String allowOrigins;
  
      @Bean
      public CorsFilter corsFilter() {
          CorsConfiguration config = new CorsConfiguration();
          config.setAllowCredentials(true);
  
          // 处理允许的源
          if (allowOrigins != null && !allowOrigins.trim().isEmpty() && !allowOrigins.trim().equals("*")) {
              config.setAllowedOriginPatterns(Arrays.asList(allowOrigins.split(",")));
          } else {
              // 如果配置为空或为*，允许所有源
              config.addAllowedOriginPattern("*");
          }
  
          config.addAllowedHeader("*");
          config.addAllowedMethod("*");
          config.setMaxAge(3600L); // 预检请求缓存时间
  
          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          source.registerCorsConfiguration("/**", config);
          return new CorsFilter(source);
      }
  }
  