CXF RESTful Client - How to Perform Basic HTTP Authentication Without Spring?

I am familiar with using Jersey to create RESTful webservice servers and clients, but due to class loading issues , I am trying to convert the Jersey client to CXF. I believe that I want to use an HTTP-oriented client, but we do not use Spring. We need to use basic HTTP authentication. The manual provides an example:

WebClient client = WebClient.create("http:books", "username", "password", "classpath:/config/https.xml"); 

The first parameter is not a URI string. Is this the format used by Spring? Can this method be used to create WebClients using Spring?

Another way to show authentication is to add a header line:

 String authorizationHeader = "Basic " + org.apache.cxf.common.util.Base64Utility.encode("user:password".getBytes()); webClient.header("Authorization", authorizationHeader); 

I assume that "user:password" should be replaced with real values, but would be grateful for the confirmation.

+4
source share
1 answer

This answer came from the CXF user mailing list.

In the first example mentioned above, there was a typo. It has been updated to:

 WebClient client = WebClient.create("http://books", "username", "password", "classpath:/config/https.xml"); 

The fourth argument may be null if the Spring configuration file (and therefore Spring) is not used.

So this worked for me:

 private WebClient webClient; public RESTfulClient(String url, String username, String password) throws IllegalArgumentException { this.username = username; this.password = password; this.serviceURL = url; if (username == null || password == null || serviceURL == null) { String msg = "username, password and serviceURL MUST be defined."; log.error(msg); throw new IllegalArgumentException(msg); } webClient = WebClient.create(this.serviceURL, this.username, this.password, null); // Spring config file - we don't use this } 
+7
source

All Articles