title: SOAP client request with username and password in header
body: <p>I have the the following Java client for making a SOAP request:</p>
  
  <pre><code>package com.example.petstore.test;
  
  import java.util.GregorianCalendar;
  
  import javax.xml.datatype.DatatypeConfigurationException;
  import javax.xml.datatype.DatatypeFactory;
  import javax.xml.datatype.XMLGregorianCalendar;
  
  import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  
  import com.example.petstore.schema.ProcessUpdateResponse;
  import com.example.petstore.schema.SyncProcessDAO;
  
  public class TestUtility {
  
      public static void main(String[] args) {
  
          JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();        
  
          // Use the URL defined in the soap address portion of the WSDL
          factory.setAddress("http://192.168.1.4:8080/MySyncService/services/SyncProcessDAOPort"); 
  
          // Utilize the class which was auto-generated by Apache CXF wsdl2java
          factory.setServiceClass(SyncProcessDAO.class);        
  
          Object client = factory.create();
  
          try {        
  
              // Call the Web Service to perform an operation
              GregorianCalendar gregory = new GregorianCalendar();
              XMLGregorianCalendar xmlgregory = DatatypeFactory.newInstance()
                      .newXMLGregorianCalendar(gregory);
              ProcessUpdateResponse response = ((SyncProcessDAO)client).gatherFunctionAttributes("hello1", "hello2", "hello3", 1, 2, xmlgregory, xmlgregory, "hello4", "hello5");    
               System.out.println("hahahaha");
              System.out.println(response);    
  
            } catch (SecurityException e) {
  
              e.printStackTrace();
  
            } catch (IllegalArgumentException e) {
  
              e.printStackTrace();
  
            } catch (DatatypeConfigurationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }        
      }
  }
  </code></pre>
  
  <p>I need to modify this to include a username and password as part of the WS security header. How would I go about doing this ?</p>
  
  <p>In case it is useful I have also attached my WSDL file:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;wsdl:definitions name="SyncProcessDAOService" targetNamespace="http://example.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"&gt;
    &lt;wsdl:types&gt;
      &lt;schema xmlns="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;import namespace="http://example.com/" schemaLocation="my_schema1.xsd"/&gt;
  &lt;/schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="gatherFunctionAttributesResponse"&gt;
      &lt;wsdl:part name="parameters" element="tns:gatherFunctionAttributesResponse"&gt;
      &lt;/wsdl:part&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="gatherFunctionAttributes"&gt;
      &lt;wsdl:part name="parameters" element="tns:gatherFunctionAttributes"&gt;
      &lt;/wsdl:part&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="SyncProcessDAO"&gt;
      &lt;wsdl:operation name="gatherFunctionAttributes"&gt;
        &lt;wsdl:input name="gatherFunctionAttributes" message="tns:gatherFunctionAttributes"&gt;
      &lt;/wsdl:input&gt;
        &lt;wsdl:output name="gatherFunctionAttributesResponse" message="tns:gatherFunctionAttributesResponse"&gt;
      &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="SyncProcessDAOServiceSoapBinding" type="tns:SyncProcessDAO"&gt;
      &lt;soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/&gt;
      &lt;wsdl:operation name="gatherFunctionAttributes"&gt;
        &lt;soap:operation soapAction="" style="document"/&gt;
        &lt;wsdl:input name="gatherFunctionAttributes"&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output name="gatherFunctionAttributesResponse"&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="SyncProcessDAOService"&gt;
      &lt;wsdl:port name="SyncProcessDAOPort" binding="tns:SyncProcessDAOServiceSoapBinding"&gt;
        &lt;soap:address location="http://localhost:8080/MySyncService/services/SyncProcessDAOPort"/&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>