Spring Gateway网关配置跨域
package com.atguigu.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;
@Configuration
public class CorsFilterConfiguration {
@Bean
public CorsWebFilter corsWebFilter() {
// 注意使用org.springframework.web.cors.reactive包下的
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}
但是在使用SpringBoot2.6版本时,需要注意:
在将allowCredentials设置为true时,需要将corsConfiguration.addAllowedOrigin("*");替换为corsConfiguration.addAllowedOriginPattern("*");
否则会报错:


浙公网安备 33010602011771号