title: CORS failure even though the headers are setup in dropwizard application?
body: <p>I am using dropwizard and angular as my UI . My server and my UI are running on different ports. My dropwizard application doesn't seem to set the Origin headers in the response. </p>
  
  <p><strong>No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' is therefore not allowed access.</strong> </p>
  
  <p>Please can you advice what could be the issue </p>
  
  <p>I have setup CORS on dropwizard as follows</p>
  
  <pre><code> @Override
  public void run(MyAppConfiguration myAppConfiguration, Environment environment) throws Exception {
      //Force browsers to reload all js and html files for every request as angular gets screwed up
      environment.servlets()
              .addFilter("CacheBustingFilter", new CacheBustingFilter())
              .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
  
      enableCorsHeaders(environment);
  
  }
  
  
  private void enableCorsHeaders(Environment env) {
          final FilterRegistration.Dynamic cors = env.servlets().addFilter("CORS", CrossOriginFilter.class);
  
          // Configure CORS parameters
                 corsFilter.setInitParameter("Access-Control-Allow-Credentials", "true");
      corsFilter.setInitParameter("Access-Control-Allow-Origin", "*");
      corsFilter.setInitParameter("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Headers, Access-Control-Request-Method, Cache-Control, Pragma, Expires");
      corsFilter.setInitParameter("Access-Control-Allow-Methods\" ", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
  
  
          // Add URL mapping
          cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
      }
  </code></pre>
  
  <p><strong>Angular Service calling into the REST endpoint</strong></p>
  
  <pre><code> private reconUrl = "http://localhost:8199/api/iceberg/reconciliations";
  
    getReconciliations(): Promise&lt;Reconciliation[]&gt; {
      return this.http.get(this.reconUrl)
        .toPromise()
        .then(response =&gt; response.json().data as Reconciliation[])
        .catch(this.handleError);
    }
  </code></pre>
  
  <p><strong>Request-Response Headers</strong></p>
  
  <p><a href="http://localhost:8199/api/iceberg/reconciliations" rel="nofollow noreferrer">http://localhost:8199/api/iceberg/reconciliations</a></p>
  
  <pre><code>GET http://localhost:8199/api/iceberg/reconciliations
  Accept: application/json, text/plain, */*
  Origin: http://localhost:4200
  X-DevTools-Emulate-Network-Conditions-Client-Id: 90d7ac77-f45f-4d60-a667-a56da9e0582b
  X-DevTools-Request-Id: 7836.4077
  User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
  Referer: http://localhost:4200/dashboard
  Accept-Encoding: gzip, deflate, br
  Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
  
  HTTP/1.1 401 Unauthorized
  Date: Thu, 06 Jul 2017 10:59:14 GMT
  WWW-Authenticate: BASIC realm="application"
  Content-Length: 0
  </code></pre>
  
  <p><strong>PreFlight OPTIONS request/response</strong></p>
  
  <p>My browser is not doing any pre-flight requests so I dont see any OPTIONS request-response. </p>
  
  <p>But I tried CURL with OPTIONS command as below and still see the same issue of 410 Unauthorized request</p>
  
  <pre><code>$ curl -H "Origin: http://example.com"        
  -H "Access-Control-Request-Method: POST"        
  -H "Access-Control-Request-Headers: X-Requested-With"        
  -X OPTIONS --verbose http://localhost:8199/api/iceberg/reconciliations
  </code></pre>
  
  <p><strong>Curl Command Request-Response below</strong></p>
  
  <pre><code> * STATE: INIT =&gt; CONNECT handle 0x6000578f0; line 1410 (connection #-5000)
      * Added connection 0. The cache now contains 1 members
      * STATE: CONNECT =&gt; WAITRESOLVE handle 0x6000578f0; line 1446 (connection #0)
      *   Trying ::1...
      * TCP_NODELAY set
      * STATE: WAITRESOLVE =&gt; WAITCONNECT handle 0x6000578f0; line 1527 (connection #0)
      * Connected to localhost (::1) port 8199 (#0)
      * STATE: WAITCONNECT =&gt; SENDPROTOCONNECT handle 0x6000578f0; line 1579 (connection #0)
      * Marked for [keep alive]: HTTP default
      * STATE: SENDPROTOCONNECT =&gt; DO handle 0x6000578f0; line 1597 (connection #0)
      &gt; OPTIONS /api/iceberg/reconciliations HTTP/1.1
      &gt; Host: localhost:8199
      &gt; User-Agent: curl/7.54.1
      &gt; Accept: */*
      &gt; Origin: http://example.com
      &gt; Access-Control-Request-Method: POST
      &gt; Access-Control-Request-Headers: X-Requested-With
      &gt;
      * STATE: DO =&gt; DO_DONE handle 0x6000578f0; line 1676 (connection #0)
      * STATE: DO_DONE =&gt; WAITPERFORM handle 0x6000578f0; line 1801 (connection #0)
      * STATE: WAITPERFORM =&gt; PERFORM handle 0x6000578f0; line 1811 (connection #0)
      * HTTP 1.1 or later with persistent connection, pipelining supported
      &lt; HTTP/1.1 401 Unauthorized
      &lt; Date: Thu, 06 Jul 2017 10:53:52 GMT
      &lt; WWW-Authenticate: BASIC realm="application"
      &lt; Content-Length: 0
  </code></pre>