How to send NTLM authenticated post request using jersey?(如何使用球衣发送经过 NTLM 身份验证的发布请求?)
问题描述
我正在使用 Java 对 Sharepoint 2013 进行 rest api 调用.如何使用 jersey rest 客户端连接到 sharepoint 2013?
I am making rest api calls to Sharepoint 2013 using Java. How can I connect to the sharepoint 2013 using jersey rest client?
注意:目前我正在使用 apache http 组件和 NTCredentials
类
Note: currently I am using apache http components and NTCredentials
class
Credentials credentials=new NTCredentials(username, password, workstation, domain);
AuthScope authScope=new AuthScope(AuthScope.ANY);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope,credentials);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
如何将其应用于 Jersey 框架?
How to adopt this to Jersey framework?
推荐答案
以下代码使用 Jersey 执行 NTLM 认证的 HTTP GET 请求:
The following code executes an NTLM authenticated HTTP GET request that using Jersey:
public Response executeRestGet(String user, String pass) {
Client client = ClientBuilder.newClient(prepareClientConfig(user, pass));
WebTarget target = client.target("http://localhost/").path("site/_api/xxxxx");
return target.request(HTTP_ACCEPT_JSON).get();
}
private ClientConfig prepareClientConfig(String user, String pass) {
ClientConfig clientConfig = new ClientConfig();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//make sure to supply all 4 arguments to the NTCredentials constructor
credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, pass, null, null));
clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
clientConfig.connectorProvider(new ApacheConnectorProvider());
return clientConfig;
}
请注意,这种方法需要:jersey-apache-connector
.Maven 依赖:
Please note that this approach requires: jersey-apache-connector
. Maven Dependency:
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.22.2</version>
</dependency>
这篇关于如何使用球衣发送经过 NTLM 身份验证的发布请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用球衣发送经过 NTLM 身份验证的发布请求?
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01