Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create NTLM Authentification with Retrofit

Since 23 sdk Android class were excluded classes:

org.apache.http.auth.AuthScheme;
org.apache.http.auth.AuthSchemeFactory;
org.apache.http.impl.auth.NTLMScheme;
org.apache.http.impl.auth.NTLMEngine;
org.apache.http.impl.auth.NTLMEngineException;

As it is now authorized in AD, with login and password through a retrofit? There OKHttpklient can be through headers?

like image 950
Vadim Seleznev Avatar asked Feb 25 '16 06:02

Vadim Seleznev


1 Answers

1) add package org.apache.httpcomponents:httpclient:4.5 to build.gradle (app)

//noinspection DuplicatePlatformClasses
implementation '**org.apache.httpcomponents:httpclient:4.5**'

2) Add package org.apache.http.impl.auth to your project (folder in /java)

3) Create public class in added org.apache.http.impl.auth package

public class PublicNTLMEngineImpl implements NTLMEngine {
  // with content of http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.5.2/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java
}

4) use Giohji's NTLMAuthenticator with instance of new PublicNTLMEngineImpl

OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .authenticator(new NTLMAuthenticator(username, password, domainOrComputerName))
                .build();

5) source code: http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.5.2/httpclient/src/main/java/org/apache/http/impl/auth/

like image 92
Roman M Avatar answered Oct 25 '22 02:10

Roman M