Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF RESTful Client - How to do 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 a Jersey client into CXF. I believe I want to use an HTTP-centric client but we don't use Spring. We need to use basic HTTP authentication. The user guide has this example:

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

The first parameter isn't a URI string. Is it a format used by Spring? Can this method only be used to create WebClients using Spring?

The other way of doing authentication shown is to add a header string:

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

I am guessing that "user:password" should be substituted with the real values, but would appreciate confirmation.

like image 534
sdoca Avatar asked Oct 24 '11 19:10

sdoca


People also ask

How do I add basic auth in HTTP request?

Basic Auth:The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.

How is basic authentication implemented in Java?

Basic authentication is a simple authentication method. Clients can authenticate via username and password. These credentials are sent in the Authorization HTTP header in a specific format. It begins with the Basic keyword, followed by a base64-encoded value of username:password.

What is http Conf conduit?

public abstract class HTTPConduit extends AbstractConduit implements Configurable, Assertor, PropertyChangeListener. This Conduit handles the "http" and "https" transport protocols. An instance is governed by policies either explicitly set or by configuration.


1 Answers

This answer came from the CXF users mailing list.

The first example referenced above had a typo in it. It has been updated to:

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

The fourth argument can be null if a Spring config file is (and therefore Spring) is not being 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
}
like image 132
sdoca Avatar answered Sep 18 '22 18:09

sdoca