Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HTTP BasicScheme.authenticate deprecated?

In Apache HTTP Component 4 class org.apache.http.impl.auth.BasicScheme I noticed that the method:

public static Header authenticate(
            final Credentials credentials,
            final String charset,
            final boolean proxy)

Is deprecated with the following information:

/**
 * Returns a basic <tt>Authorization</tt> header value for the given
 * {@link Credentials} and charset.
 *
 * @param credentials The credentials to encode.
 * @param charset The charset to use for encoding the credentials
 *
 * @return a basic authorization header
 *
 * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}.
 */
@Deprecated

However, I see no document explaining how to migrate from the deprated function to the new function. Although the deprecated function works, I would rather do things the "right" way. Here is how I am using the deprecated function:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpLogin = new HttpPost(uriLogin);
hpLogin.setHeader(BasicScheme.authenticate(creds, "US-ASCII", false));

How could I take this same concept and apply it to the "right" method for BasicScheme.authenticate?

like image 618
E.S. Avatar asked Nov 05 '13 19:11

E.S.


1 Answers

complementing oleg's answer, here's an example replacement that worked in my needs.

Notice the need to go from a static call to an object instance.

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpPost = new HttpPost(uriLogin);
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(creds , hpPost, null);
hpPost.addHeader( header); 
like image 149
Matt S. Avatar answered Oct 07 '22 04:10

Matt S.