How to configure a CXF-generated client for proactive HTTP authentication?

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

The problem I encountered is not able to properly configure proactive auth in the client.

I tried several things to no avail. Most of the examples on the Internet seem to focus on Spring, but on a simple old Java approach.

I include the bulk of the client. If anyone can give me an example of how this should be configured, I would appreciate it. Notice that I'm not looking for anything out of the ordinary. I just need to be able to authenticate and call 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); } } 
+1
source share
4 answers

OK, I get it. Pretty simple when it comes to it. Hope this saves someone 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"); 
+4
source

One more example:

 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); } } 
+4
source

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

How to change HTTP headers for a JAX-WS response in CXF?

0
source

Hi friend, you configured the authentication part after calling the webservice, how does it work?

 Client client = ClientProxy.getClient(port); HTTPConduit http = (HTTPConduit) client.getConduit(); http.getAuthorization().setUserName("theusername"); http.getAuthorization().setPassword("thepassword"); 
0
source

Source: https://habr.com/ru/post/945631/


All Articles