Gateway 网关 之 跨域

关于网关配置跨域的没什么可多说的,简单粗暴一回,直接上代码:

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;
 
/**
 * 解决跨域的配置类
 *
 * @author Alay
 * @date 2021-05-29 11:37
 * @project Braineex
 */
@Configuration
public class CoreOriginConfig {
 
   /**
    * Gateway 上配置 跨域问题
    *
    * @return
    */
   @Bean
   public CorsWebFilter coreWebFilter() {
      CorsConfiguration config = new CorsConfiguration();
      // * 号表示匹配任意的
      config.addAllowedMethod("*");
      config.addAllowedOrigin("*");
      config.addAllowedHeader("*");
 
      PathPatternParser patternParser = new PathPatternParser();
      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(patternParser);
      // ** 代表所有
      source.registerCorsConfiguration("/**", config);
 
      CorsWebFilter corsWebFilter = new CorsWebFilter(source);
 
      return corsWebFilter;
   }
}

 

上一篇:mercurius 基于fastify 的graphql server 以及gateway 服务


下一篇:gRPC学习之五:gRPC-Gateway实战