Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OkHTTP 3 with Digest

So in order to use digest authentication with OkHTTP I need to use the digest plugin or implement myself so naturally I am using the plugin. However the example usage on the repositories readme is for the previous version of OkHTTP and I cannot use the same code to implement it in version 3. I have tried to look at some of their tests (which use OkHTTP 3) to see how the code works but I am just not sure.

So how can I use this http digest plugin with the most recent version of OkHttp?

like image 629
user2844358 Avatar asked Apr 22 '26 01:04

user2844358


1 Answers

import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
import com.burgstaller.okhttp.digest.CachingAuthenticator;
import com.burgstaller.okhttp.digest.Credentials;
import com.burgstaller.okhttp.digest.DigestAuthenticator;
import okhttp3.OkHttpClient;

private OkHttpClient buildHttpClient(String username, String password) {
    // Library used for digest authenticaton: https://github.com/rburgst/okhttp-digest
    var authenticator = new DigestAuthenticator(new Credentials(username, password));
    var authCache = new ConcurrentHashMap<String, CachingAuthenticator>();

    var decorator = new CachingAuthenticatorDecorator(authenticator, authCache);
    var interceptor = new AuthenticationCacheInterceptor(authCache);

    return httpClient.newBuilder()
        .authenticator(decorator)
        .addInterceptor(interceptor)
        .build();
}
like image 87
Eduard Wirch Avatar answered Apr 23 '26 13:04

Eduard Wirch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!