title: Add Basic Authorization HTTP Headers to SOAP Request with Spring-WS
body: <p>I have been trying to consume a SOAP service using a Spring application, but I've been absolutely stuck for a while. I expect the solution will be very simple and I'm hoping to be pointed in the right direction.</p>
  <p>I am able to successfully make requests via SoapUI. I simply loaded the .WSDL file, selected the method to use, and added the username and password with basic authorization from the 'Auth' menu in SoapUI.</p>
  <p>In the Spring application, I've gotten to the point of getting a 401 error, so I believe it's almost there. I referenced these two nice examples below to first add the username and password, and secondly to log the HTTP headers to verify that they are populated correctly.</p>
  <p><a href="https://codenotfound.com/spring-ws-log-client-server-http-headers.html" rel="nofollow noreferrer">https://codenotfound.com/spring-ws-log-client-server-http-headers.html</a></p>
  <p><a href="https://stackoverflow.com/questions/3169237/setting-a-custom-http-header-dynamically-with-spring-ws-client">Setting a custom HTTP header dynamically with Spring-WS client</a></p>
  <p>However, neither setting the 'usernamePasswordCredentials', nor setting the connection's request header seems to have any effect. I've also confirmed that the XML body is correct by testing the logged output in SoapUI. So I believe it's just an authorization issue.</p>
  <p>Bean/Configuration Class:</p>
  <pre><code>@Bean
  public Jaxb2Marshaller marshaller() {
      System.out.println(&quot;BEAN CREATED: MARSHALLER...&quot;);
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setContextPath(&quot;...&quot;); // omitted for example
  
      return marshaller;
  }
  
  @Bean
  public UsernamePasswordCredentials usernamePasswordCredentials() {
      return new UsernamePasswordCredentials(&quot;testu&quot;, &quot;test&quot;);
  }
  
  @Bean
  @DependsOn({&quot;usernamePasswordCredentials&quot;})
  public HttpComponentsMessageSender httpComponentsMessageSender(UsernamePasswordCredentials usernamePasswordCredentials) {
      HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
      
      httpComponentsMessageSender.setCredentials(usernamePasswordCredentials);
  
      return httpComponentsMessageSender;
  }
  
  @Bean
  @DependsOn({&quot;marshaller&quot;})
  public TicketDetailsClient ticketDetailsClient(Jaxb2Marshaller marshaller) {
      System.out.println(&quot;BEAN CREATED: TICKETDETAILSCLIENT...&quot;);
      TicketDetailsClient ticketDetailsClient = new TicketDetailsClient();
      
      ticketDetailsClient.setDefaultUri(&quot;...&quot;); // omitted for example
      ticketDetailsClient.setMarshaller(marshaller);
      ticketDetailsClient.setUnmarshaller(marshaller);
  
      return ticketDetailsClient;
  }
  </code></pre>
  <p>Bean Method:</p>
  <pre><code>    public GetTicketDetailsResponse getTicketDetails(long definitionId, long itemId) {
      ObjectFactory of = new ObjectFactory();
  
      this.template.setInterceptors(new ClientInterceptor[] {new LogHttpHeaderClientInterceptor()});
      
      GetItemDetailsRequest itemDetailsRequest = new GetItemDetailsRequest();
      itemDetailsRequest.setItemDefinitionId(definitionId);
      itemDetailsRequest.setItemId(itemId);
      
      GetTicketDetails getTicketDetails = new GetTicketDetails();
      getTicketDetails.setGetItemDetailsRequest(itemDetailsRequest);
      JAXBElement&lt;GetTicketDetails&gt; elGetTicketDetails = of.createGetTicketDetails(getTicketDetails);
      
      System.out.println(&quot;ABOUT TO MARSHALSENDANDRECEIVE...&quot;);
      GetTicketDetailsResponse ticketDetailsResponse = (GetTicketDetailsResponse) this.template.marshalSendAndReceive(elGetTicketDetails);
      
      return ticketDetailsResponse;
  }
  </code></pre>
  <p>Interceptor:</p>
  <pre><code>@Override
  public boolean handleRequest(MessageContext arg0) throws WebServiceClientException {
      // TODO Auto-generated method stub
      TransportContext context = TransportContextHolder.getTransportContext();
      HttpComponentsConnection connection =(HttpComponentsConnection) context.getConnection();
      try {
          connection.addRequestHeader(&quot;username&quot;, &quot;testu&quot;);
          connection.addRequestHeader(&quot;password&quot;, &quot;test&quot;);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      HttpLoggingUtils.logMessage(&quot;Client Request Message&quot;, arg0.getRequest());
  
      return true;
  }
  </code></pre>
  <p>Result Snippet (This is where I expected to see the username/password headers. Since they are missing, I'm guessing this is the issue):</p>
  <pre><code>ABOUT TO MARSHALSENDANDRECEIVE...
  2021-08-09 13:46:18.891  INFO 23112 --- [           main] com.fp.fpcustomization.HttpLoggingUtils  : 
  ----------------------------
  Client Request Message
  ----------------------------
  Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  SOAPAction: &quot;&quot;
  Content-Type: text/xml; charset=utf-8
  Content-Length: 378
  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;SOAP-ENV:Header/&gt;&lt;SOAP-ENV:Body&gt;&lt;ns3:getTicketDetails xmlns:ns3=&quot;http://externalapi.business.footprints.numarasoftware.com/&quot;&gt;&lt;getItemDetailsRequest&gt;&lt;_itemDefinitionId&gt;76894&lt;/_itemDefinitionId&gt;&lt;_itemId&gt;30201&lt;/_itemId&gt;&lt;/getItemDetailsRequest&gt;&lt;/ns3:getTicketDetails&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;
  
  [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.151 s &lt;&lt;&lt; FAILURE! - in com.fp.fpcustomization.FpcustomizationApplicationTests
  [ERROR] RequestTest  Time elapsed: 0.51 s  &lt;&lt;&lt; ERROR!
  org.springframework.ws.client.WebServiceTransportException:  [401]
      at com.fp.fpcustomization.FpcustomizationApplicationTests.RequestTest(FpcustomizationApplicationTests.java:29)
  </code></pre>