1. 写个配置类 CorsConfig (如果不行,能够不写此类,和 springcloud 的版本无关)
package net.youqu.micro.service.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;
/**
- description:
- java 页游我的项目 www fhadmin org
*/
public class CorsConfig {
public CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = www.sangpi.comnew UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
- 配置 application.properties 形式
——–gateway 配置 ——–
跨域配置
spring.cloud.gateway.globalcors.corsConfigurations.[/*].allowedOrigins=
spring.cloud.gateway.globalcors.corsConfigurations.[/*].allowedHeaders=
spring.cloud.gateway.globalcors.corsConfigurations.[/*].allowedMethods=
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=true
yaml 形式
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowCredentials: true
allowedHeaders: '*'
allowedMethods: '*'
allowedOrigins: '*'