package com.zhentao.common; /** * @date: 2025/5/26 14:15 * @author: ftt */ 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; @Configuration public class CorsConfig { @Bean public CorsFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("http://localhost:8080"); // 允许的前端域名 config.addAllowedMethod("*"); // 允许的HTTP方法(GET/POST等) config.addAllowedHeader("*"); // 允许的请求头 config.setAllowCredentials(true); // 允许携带Cookie UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } }