如何使用泽西发送NTLM认证的帖子请求? [英] How to send NTLM authenticated post request using jersey?

查看:142
本文介绍了如何使用泽西发送NTLM认证的帖子请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java对Sharepoint 2013进行休息api调用。如何使用泽西休息客户端连接到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 class

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 - 连接器。 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认证的帖子请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆