网关跨域配置

This commit is contained in:
panxuejie 2026-01-05 14:37:24 +08:00
parent 142a9a2456
commit 9d0103861f
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.tacit.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 java.util.Collections;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfig = new CorsConfiguration();
// 允许所有来源
corsConfig.addAllowedOriginPattern("*");
// 允许所有 HTTP方法
corsConfig.setAllowedMethods(Collections.singletonList("*"));
// 允许所有请求头
corsConfig.setAllowedHeaders(Collections.singletonList("*"));
// 允许携带凭证
corsConfig.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// 对所有路径生效
source.registerCorsConfiguration("/**", corsConfig);
return new CorsWebFilter(source);
}
}