Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 Encoding Basic Authentication Header Apache HTTP Client

Two related questions, I'm using Apache HTTP Client 4.x API. myHttpPost is an instance of HttpPost and myHttpClient is an instance of HttpClient. I'm trying to send a request using basic authentication. So I have a HttpClient and create a HttpPost.

The 'brute force' way of setting a basic authentication header seems to be to set it in the HttpPost header.

String encoding = Base64Encoder.encode("username" + ":" + "password");
myHttpPost.setHeader("Authorization", "Basic " + encoding);

The example above came from another stack overflow question (can't find link to it now). In relation to the Base64Encoder class - which package would I find it in or where would I download it from?

Main question - I was hoping to do basic authentication in a more aesthetic manner using the code below:

myHttpClient.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
    new UsernamePasswordCredentials("username", "password")
);

But this doesn't seem to work. So is the first example above the right way to do basic authentication with Apache HTTP Client 4.0? Or is there a cleaner/simpler way.

like image 778
gamozzii Avatar asked Dec 01 '11 08:12

gamozzii


People also ask

How do I add basic authentication to HttpClient Java?

Firstly, we create an HttpClient, which can be used to execute HTTP requests. Secondly, we create an HttpRequest using the builder design pattern. The GET method sets the HTTP method of the request. The uri method sets the URL where we would like to send the request.

Does Basic Auth use base64?

RFC 2617 requires that in HTTP Basic authentication, the username and password must be encoded with base64. To receive authorization, the client sends the userid and password, separated by a single colon (":") character, within a base64 encoded string in the credentials.

What is HTTP basic authentication header?

HTTP basic authentication is a simple challenge and response mechanism with which a server can request authentication information (a user ID and password) from a client. The client passes the authentication information to the server in an Authorization header. The authentication information is in base-64 encoding.


1 Answers

In relation to the Base64Encoder class - which package would I find it in or where would I download it from?

Base64Encoder can come from various places, I couldn't find something that matches with your static encode method.

As for Credentials, you need to set scheme to Basic on your AuthScope, like so:

myHttpClient.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "basic"),
    new UsernamePasswordCredentials("username", "password")
);

or

myHttpClient.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
    new UsernamePasswordCredentials("username", "password")
);
like image 200
Buhake Sindi Avatar answered Sep 20 '22 00:09

Buhake Sindi