title: Spring Global CORS configuration not working but Controller level config does
body: <p>I am trying to configure CORS globally via <code>WebMvcConfigurerAdapter</code> shown below. To test I am hitting my API endpoint via a small node app I created to emulate an external service. When I try this approach the response does not contain the correct headers and fails with </p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:8080/api/query/1121. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:333' is therefore not allowed access.
  </code></pre>
  
  <p>Global Config</p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.config.annotation.CorsRegistry;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @EnableWebMvc
  @Configuration
  public class WebConfig extends WebMvcConfigurerAdapter {
          @Override
          public void addCorsMappings(CorsRegistry registry) {
              registry.addMapping("/api/query/**")
                      .allowedOrigins("*")
                      .allowedHeaders("*")
                      .allowCredentials(true);
          }
  }
  </code></pre>
  
  <p>However when I utilize the <code>@CrossOrigin</code> annotation like so it works just fine responding with the proper headers.</p>
  
  <pre><code>@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
  @RestController
  @RequestMapping(value = "/api/query", produces = MediaType.APPLICATION_JSON_VALUE)
  public class QueryController {
     ......
  }
  </code></pre>
  
  <p>Produces</p>
  
  <pre><code>Access-Control-Allow-Credentials:true
  Access-Control-Allow-Origin:http://localhost:333
  </code></pre>
  
  <p>What am I missing to make the global config work (followed instructions here <a href="https://spring.io/blog/2015/06/08/cors-support-in-spring-framework">https://spring.io/blog/2015/06/08/cors-support-in-spring-framework</a>).  I feel like I'm missing something simple since annotating the controller works just fine.</p>