自23 sdk Android类被排除在外的类:
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;
现在已经在AD中获得授权,通过改造登录和密码? OKHttpklient可以通过标题吗?
答案 0 :(得分:0)
我在okhttp's github找到了答案。 它由SelvinPL发布。
首先,您必须实现NTLM身份验证器(它使用NTLMEngineImpl,org.apache.http.impl.auth.NTLMEngineImpl的独立版本,也由SelvinPL创建)。下面的代码是SelvinPL实现的略微修改版本,可以在最新的改造版本(2.1.0)上运行。
Row
然后你可以像这个例子一样注册验证者:
private static class NTLMAuthenticator implements Authenticator {
final NTLMEngineImpl engine = new NTLMEngineImpl();
private final String domain;
private final String username;
private final String password;
private final String ntlmMsg1;
private NTLMAuthenticator(String username, String password, String domain) {
this.domain = domain;
this.username = username;
this.password = password;
String localNtlmMsg1 = null;
try {
localNtlmMsg1 = engine.generateType1Msg(null, null);
} catch (Exception e) {
e.printStackTrace();
}
ntlmMsg1 = localNtlmMsg1;
}
@Override
public Request authenticate(Route route, Response response) throws IOException {
final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
if (WWWAuthenticate.contains("NTLM")) {
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
}
String ntlmMsg3 = null;
try {
ntlmMsg3 = engine.generateType3Msg(username, password, domain, "android-device", WWWAuthenticate.get(0).substring(5));
} catch (Exception e) {
e.printStackTrace();
}
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
}
}
适用于com.squareup.retrofit2:retrofit:2.1.0。
答案 1 :(得分:0)
1)将软件包 org.apache.httpcomponents:httpclient:4.5 添加到build.gradle(应用程序)
//noinspection DuplicatePlatformClasses
implementation '**org.apache.httpcomponents:httpclient:4.5**'
2)将包 org.apache.http.impl.auth 添加到您的项目(/ java中的文件夹)
3)在添加的 org.apache.http.impl.auth 包中创建公共类
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)将Giohji的 NTLMAuthenticator 与新的 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();