httpclient实现ntlm认证(window)

httpclient版本 4.5

写道
import java.nio.charset.Charset;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


public class SCORunbookTest {
private void test() throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();

NTCredentials creds = new NTCredentials("user", "password", "", "");
credsProvider.setCredentials(new AuthScope("10.0.10.76", 81, null, "ntlm"), creds);
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpHost target = new HttpHost("10.0.10.76", 81, "http");
// 保证相同的内容来用于执行逻辑相关的请求
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("/Orchestrator2012/Orchestrator.svc/");
CloseableHttpResponse response = httpclient.execute(target, httpGet, localContext);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}

 

HttpClient可以通过提供的CredentialsProvider接口来实现digest认证。下面是一个示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class DigestAuthExample { public static void main(String[] args) throws IOException { String username = "user"; String password = "password"; HttpHost target = new HttpHost("localhost", 8080, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password)); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); HttpClientContext context = HttpClientContext.create(); HttpGet httpGet = new HttpGet("/"); CloseableHttpResponse response = httpClient.execute(target, httpGet, context); try { HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { response.close(); } // Get the digest scheme from the context DigestScheme digestScheme = (DigestScheme) context.getAuthScheme(target); // Use the digest scheme to generate the next request's headers HttpGet httpGet2 = new HttpGet("/"); httpGet2.addHeader(digestScheme.authenticate( new UsernamePasswordCredentials(username, password), httpGet2, context)); CloseableHttpResponse response2 = httpClient.execute(target, httpGet2, context); try { HttpEntity entity = response2.getEntity(); EntityUtils.consume(entity); } finally { response2.close(); } } } ``` 这个示例代码中,使用了BasicCredentialsProvider来提供用户名和密码。当httpClient执行第一次请求时,服务器返回401 Unauthorized响应,httpClient会尝试使用提供的凭据进行认证。然后,httpClient会使用DigestScheme生成下一个请求的摘要认证头。在第二次请求中,httpClient使用这个头来进行认证
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值