title: Spring Gateway Request blocked by CORS (No Acces0Control-Allow-Orgin header)
body: <p>I have a <strong>Angular frontend</strong>, <strong>spring cloud gateway</strong> and a <strong>spring web service</strong>. When I try to send GET/POST data to the spring web service through the gateway I get the following error: <a href="https://i.stack.imgur.com/qRyK9.png" rel="noreferrer">CORS error</a>. When sending the data directly to the web service it works fine so I think the problem is in the gateway.</p>
  
  <p>In the gateway I have to following files:</p>
  
  
  
  <pre><code>@Configuration
  @CrossOrigin(origins = "*")
  public class SpringCloudConfig {
  
      @Bean
      public RouteLocator gatewayRoutes(RouteLocatorBuilder builder){
          return builder.routes()
                  .route(r -&gt; r.path("/users/**")
                  .uri("http://localhost:8081/")
                  .id("userService"))
  
                  .route(r -&gt; r.path("/posts/**")
                          .uri("http://localhost:8082/")
                          .id("postService"))
  
                  .route(r -&gt; r.path("/auth/**")
                          .uri("http://localhost:8083/")
                          .id("securityService"))
                  .build();
      }
  
  }
  </code></pre>
  
  <p><strong>application.properties:</strong> I thought the server: cloud: etc etc.. would do the trick but no</p>
  
  
  
  <pre><code>server.port=8080
  
  spring:
  cloud:
  gateway:
  globalcors:
  corsConfigurations:
  '[/**]':
  allowedOrigins: "*"
  allowedMethods:
  - GET
  - POST
  </code></pre>
  
  <p><strong>Pom.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;com.cloudGateway&lt;/groupId&gt;
  &lt;artifactId&gt;gateway&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;gateway&lt;/name&gt;
  &lt;description&gt;Gateway project for Spring Boot&lt;/description&gt;
  
  &lt;properties&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;spring-cloud.version&gt;Hoxton.SR3&lt;/spring-cloud.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-gateway&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                  &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
              &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>CorsConfiguration File:</p>
  
  <pre><code>package com.cloudGateway.gateway;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.cors.reactive.CorsWebFilter;
  import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
  
  import java.util.Arrays;
  import java.util.Collections;
  
  @Configuration
  public class CorsConfiguration extends org.springframework.web.cors.CorsConfiguration {
  
      @Bean
      public CorsWebFilter corsWebFilter() {
  
      final CorsConfiguration corsConfig = new CorsConfiguration();
      corsConfig.setAllowedOrigins(Collections.singletonList("*"));
      corsConfig.setMaxAge(3600L);
      corsConfig.setAllowedMethods(Arrays.asList("GET", "POST"));
      corsConfig.addAllowedHeader("*");
  
      final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      source.registerCorsConfiguration("/**", corsConfig);
  
      return new CorsWebFilter(source);
  }
  </code></pre>
  
  <p><strong>Gateway Repo:</strong> <a href="https://github.com/KylevanRaaij/Gateway" rel="noreferrer">https://github.com/KylevanRaaij/Gateway</a></p>
  
  <p><strong>Service to connect to:</strong> <a href="https://github.com/KylevanRaaij/UserService" rel="noreferrer">https://github.com/KylevanRaaij/UserService</a> (this one works when connecting direct) (for example my angular project)</p>