Search code examples
javaauthenticationsoapcxfpreemptive

How does one configure CXF generated client for preemptive HTTP auth?


I have a client that was generated by CXF using a local wsdl file. The client connects OK, and I get an expected 401 error from the web server.

The problem I've run into is not being able to properly configure preemptive auth in the client.

I've tried a number of things to no avail. The majority of examples on the web seem to focus on Spring, rather a plain old Java approach.

I'm including the main portion of the client. If anyone can give me an example of how this should be configured, I'd appreciate it. Note that I'm not looking for anything fancy. I just need to be able to authenticate and call the services.

public final class ServiceNowSoap_ServiceNowSoap_Client {

private static final QName SERVICE_NAME = new QName(
        "http://www.service-now.com/foo",
        "ServiceNow_foo");

private ServiceNowSoap_ServiceNowSoap_Client() {
}

public static void main(String args[]) throws java.lang.Exception {
    URL wsdlURL = ServiceNowCmdbCiComm.WSDL_LOCATION;
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    ServiceNowFoo ss = new ServiceNowFoo(wsdlURL,
            SERVICE_NAME);
    ServiceNowSoap port = ss.getServiceNowSoap();

    {
        System.out.println("Invoking deleteRecord...");
        java.lang.String _deleteRecord_sysId = "";
        java.lang.String _deleteRecord__return = port
                .deleteRecord(_deleteRecord_sysId);
        System.out.println("deleteRecord.result=" + _deleteRecord__return);

    }
    System.exit(0);
}

}

Solution

  • OK, I figured this out. Pretty straightforward when it comes down to it. Hope this saves somebody a couple of minutes...

    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.xml.namespace.QName;
    
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.frontend.ClientProxy;
    import org.apache.cxf.transport.http.HTTPConduit;
    
    private static final QName SERVICE_NAME = new QName(
            "http://www.service-now.com/foo",
            "ServiceNow_foo");
    
    private ServiceNowSoap_ServiceNowSoap_Client() {
    }
    
    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = ServiceNowFoo.WSDL_LOCATION;
        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
            File wsdlFile = new File(args[0]);
            try {
                if (wsdlFile.exists()) {
                    wsdlURL = wsdlFile.toURI().toURL();
                } else {
                    wsdlURL = new URL(args[0]);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    
        ServiceNowFoo ss = new ServiceNowFoo(wsdlURL,
                SERVICE_NAME);
    
        ServiceNowSoap port = ss.getServiceNowSoap();
    
        Client client = ClientProxy.getClient(port);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.getAuthorization().setUserName("theusername");
        http.getAuthorization().setPassword("thepassword");
    
        // Do your work here.
    }