title: Authentication headers with JQuery and Tomcat
body: <p>My goal is to have a web-service, using an Apache Tomcat 7, and to communicate with it using ajax request from another domain. The web-service is working, but I'm having troubles with the JavaScript part.</p>
  
  <p>My problem is when I'm trying to do any request with basic auth, the headers are removed when the Tomcat give me the request :</p>
  
  <pre><code>String basicAuth = request.getHeader("Authorization");
  </code></pre>
  
  <p>The string is always to null. I've tried to look for answers, and I'm guessing the problem come from my web.xml configuration (and because I'm using cross-domain requests).</p>
  
  <p>The javaScript part looks like this:</p>
  
  <pre><code>$.ajax({
      type: 'GET',
      url: 'http://localhost:8080/Test/endpoint/to/test',
      dataType : 'json',
      beforeSend: function (request) {
          request.withCredentials = true;
          request.setRequestHeader("Authorization", "Basic  " + btoa('test' + ":" + 'test'));
      },
      success: function(data) {
          console.log(data);
      },
      error: function(data) {
          console.log(data);
      }
  });
  </code></pre>
  
  <p>And my web.xml file looks like this:
      </p>
  
  <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
      version="2.4"&gt;
  
      &lt;display-name&gt;File Explorer&lt;/display-name&gt;
      &lt;description&gt;
          File Explorer
      &lt;/description&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Test&lt;/servlet-name&gt;
          &lt;servlet-class&gt;fr.vuzi.controller.FrontController&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;FrontController&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.catalina.filters.CorsFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.allowed.origins&lt;/param-name&gt;
              &lt;param-value&gt;&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.allowed.methods&lt;/param-name&gt;
              &lt;param-value&gt;GET,POST,HEAD,OPTIONS,PUT&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.allowed.headers&lt;/param-name&gt;
              &lt;param-value&gt;Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.exposed.headers&lt;/param-name&gt;
              &lt;param-value&gt;Access-Control-Allow-Origin,Access-Control-Allow-Credentials&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.support.credentials&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.preflight.maxage&lt;/param-name&gt;
              &lt;param-value&gt;10&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>(I'm using only one endpoint for every request). I've tried pretty much anything I could find, and I don't know if the problem is on the client or server side.</p>
  
  <p>I know there is similar questions: <a href="https://stackoverflow.com/questions/18889983/authentication-headers-are-not-sending-jquery-ajax">Authentication headers are not sending JQuery Ajax</a> but none of the solutions seems to work for me...</p>
  
  <p>Thanks in advance.</p>