1. Escreva uma classe de configuração CorsConfig (se não, você não pode escrever este tipo, que está relacionado à versão do 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; / ** * descrição: * java 项目 www.fhadmin.org * / @Configuration public class CorsConfig { @Bean public CorsWebFilter corsFilter () { CorsConfiguration config = new CorsConfiguration (); config.addAllowedMethod ("*"); config.addAllowedOrigin ("*"); config.addAllowedHeader ("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (new PathPatternParser ()); source.registerCorsConfiguration ("/ **", config); retornar novo CorsWebFilter (fonte); } }
2. Configure o modo 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
caminho yaml
spring: cloud: gateway: globalcors: corsConfigurations: '[/ **]': allowCredentials: true allowedHeaders: '*' allowedMethods: '*' allowedOrigins: '*'