解决geteway跨域问题解决

This commit is contained in:
haozq 2025-05-21 15:34:42 +08:00
parent ab1ca78891
commit 073e4850fb
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.bonus.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
import java.util.Collections;
/**
* @author 黑子
*/
@Configuration
public class CorsConfig {
@Bean
// CorsWebFilter Spring Framework
// 用于处理跨源资源共享CORS, Cross-Origin Resource Sharing的过滤器
public CorsWebFilter corsFilter() { //网关过滤器写法基本是固定的
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*")); // 允许所有方法也可以指定如GET, POST等具体方法
config.setAllowedHeaders(Collections.singletonList("*")); // 允许所有头信息也可以指定具体头信息
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config); // 对所有路径应用此CORS配置
return new CorsWebFilter(source);
}
}