-
Notifications
You must be signed in to change notification settings - Fork 60
Basic HTTP Authentication
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:
- Per method: add a
BasicAuthCredentials
to each method that requires authentication; - Per service: use this when you have a service (interface) where all methods require 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.
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.