Vad*_*nev 6 java android ntlm retrofit
由于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;
Run Code Online (Sandbox Code Playgroud)
因为它现在在AD中被授权,通过改造登录和密码?OKHttpklient可以通过标题吗?
我在okhttp 的 github上找到了答案。它是由SelvinPL发布的。
首先,您必须实现 NTLM 身份验证器(它使用 NTLMEngineImpl,这是org.apache.http.impl.auth.NTLMEngineImpl的独立版本,它也是由SelvinPL创建的)。下面的代码是SelvinPL实现的稍微修改版本,可在最新的改进版本 (2.1.0) 上运行。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以像下面的示例一样注册身份验证器:
OkHttpClient client = new OkHttpClient.Builder()
.authenticator(new NTLMAuthenticator(username, password, domain))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getURL(context))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit.create(Api.class);
Run Code Online (Sandbox Code Playgroud)
它适用于 com.squareup.retrofit2:retrofit:2.1.0。
| 归档时间: |
|
| 查看次数: |
1796 次 |
| 最近记录: |