0

I am trying to add NTLM authentication (username and password) to a Retrofit OkHttp client.

import java.io.IOException;

import okhttp3.Credentials;

import okhttp3.Interceptor;

import okhttp3.Request;

import okhttp3.Response;


public class NTMLAuthInterceptor implements Interceptor {

    private String credentials;

    public NTMLAuthInterceptor(String user, String password) {
        this.credentials = Credentials.basic(user, password);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request authenticatedRequest = request.newBuilder()
            .header("Authorization", credentials).build();
        return chain.proceed(authenticatedRequest);
    }
}

Adding the interceptor to an OkHttp client

   OkHttpClient client = new OkHttpClient.Builder()

    .addInterceptor(new NTMLAuthInterceptor(username, password))

    .build();

but in response I am getting 401 error. Can anyone help me?

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
  • NTLM authentication is more complicated than `Basic` which you've configured there: https://techcommunity.microsoft.com/t5/iis-support-blog/windows-authentication-http-request-flow-in-iis/ba-p/324645 – msbit May 26 '21 at 01:45
  • You could take a look at the proof of concept attached to https://github.com/square/okhttp/issues/206 for an idea of where to start. – msbit May 26 '21 at 01:47

0 Answers0