如何使用球衣发送经过 NTLM 身份验证的发布请求?

How to send NTLM authenticated post request using jersey?(如何使用球衣发送经过 NTLM 身份验证的发布请求?)

本文介绍了如何使用球衣发送经过 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 身份验证的发布请求?

基础教程推荐