Skip to content

Basic HTTP Authentication

mmazi edited this page Sep 23, 2014 · 3 revisions

Rescu provides support for Basic HTTP Authentication. If the server you are connecting to with rescu requires Basic Authentication, here's what you need to do.

There are two ways to do it:

  1. Per method: add a BasicAuthCredentials to each method that requires authentication;
  2. Per service: use this when you have a service (interface) where all methods require authentication.

Per method authentication

Add an extra parameter to all methods that implement the API calls that require Basic Authentication:

 @HeaderParam("Authorization") BasicAuthCredentials basicAuth

The name of the parameter doesn't matter, but it must be annotated with @HeaderParam("Authorization").

Provide this parameter when calling the method:

new BasicAuthCredentials("username", "password")

That's all. See ExampleService and the test for an example with some more context.

Per service authentication

When you have a service interface where all methods require authentication, you can configure the service to authenticate for all methods:

    final ClientConfig config = new ClientConfig();
    ClientConfigUtil.addBasicAuthCredentials(config, username, password);
    RestProxyFactory.createProxy(ServiceInterface.class, "https://api.example.com/", config)

This way you don't need to declare any @HeaderParam("Authorization") BasicAuthCredentials with the individual methods.

See the test for an example.