@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(Collections.singletonList("*"));
// config.addAllowedOrigin("http://localhost:5173"); // 允许从特定域名访问接口
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowCredentials(true);
config.setAllowedHeaders(Collections.singletonList("*"));
// config.addAllowedHeader("http://localhost:5173/"); // 允许所有请求头
// config.addAllowedMethod("*"); // 允许 POST 请求
// config.addAllowedMethod("GET"); // 允许 GET 请求
// config.addAllowedMethod("OPTIONS"); // 允许 OPTIONS 请求
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
解决跨域CorsFilter
最新推荐文章于 2024-07-18 20:13:39 发布