title: Add Http Basic Authentication to servletRequest
body: <p>I have a <code>ProxyServlet</code> to handle requests to another server (which uses HTTP Basic Authentication) sent from my Application, and i want to add the header manually before the Servlet fires the actual request so the User wont have to enter any credentials.</p>
  
  <p>I have tried something like this code below using <code>HttpServletRequestWrapper</code></p>
  
  <pre><code>public class DataServlet extends ProxyServlet {
    @Override
    protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
          throws ServletException, IOException {
  
      final SystemCredentials credentials = new SystemCredentials("username", "password");
  
      HttpServletRequestWrapper wrap = new HttpServletRequestWrapper(servletRequest){
  
          @Override
          public String getHeader(String name) {              
              if (name.equals("Authorization")){
                  String encoding = Base64.getEncoder().encodeToString((credentials.getUser().concat(":").concat(credentials.getPassword()).getBytes()));
                  return "Basic " + encoding;
              } else
                  return super.getHeader(name);
          }
  
      };
  
      super.service(wrap, servletResponse);
  
    }
  }
  </code></pre>
  
  <p>It doesnt seem to work, when i try to access it, shows a pop-up and asks for credentials for the remote server.</p>
  
  <p>My web.xml contains</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;data&lt;/servlet-name&gt;
    &lt;servlet-class&gt;foo.package.servlet.DataServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;targetUri&lt;/param-name&gt;
      &lt;param-value&gt;http://fooServer/DataServer&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;async-supported&gt;true&lt;/async-supported&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;data&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/DataServer/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Is there any other way to make this work? </p>
  
  <p>Thanks!</p>