Question

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);
}

}
Was it helpful?

Solution 2

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.
}

OTHER TIPS

Another aproach would be:

import javax.xml.ws.BindingProvider;

public class CxfClientExample {

    public static void main(String[] args) throws Exception {
        String endPointAddress = "http://www.service-now.com/foo";
        ServiceNowFoo service = new ServiceNowFoo();
        ServiceNowFooPortType port = service.getServiceNowFoo();

        BindingProvider bindingProvider = (BindingProvider) port;
        bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endPointAddress);
        bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "theusername");
        bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "thepassword");

        String deleteRecord_return = port.deleteRecord("");
        System.out.println("deleteRecord.result=" + deleteRecord_return);    
    }
}

You can also use interceptors. One benefit of using interceptor is that you can attach it to all your clients , streamlining your pre-emptive authentication approach. Take a look at :

How do i modify HTTP headers for a JAX-WS response in CXF?

Hi Friend you have configured the authentication part after invoking the webservice, how does this works ?

Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getAuthorization().setUserName("theusername");            
http.getAuthorization().setPassword("thepassword");        
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top