Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用jersey发送经过NTLM验证的post请求?_Java_Rest_Jersey_Sharepoint 2013_Ntlm - Fatal编程技术网

Java 如何使用jersey发送经过NTLM验证的post请求?

Java 如何使用jersey发送经过NTLM验证的post请求?,java,rest,jersey,sharepoint-2013,ntlm,Java,Rest,Jersey,Sharepoint 2013,Ntlm,我正在使用Java对Sharepoint 2013进行rest api调用。如何使用jersey rest客户端连接到sharepoint 2013 注意:目前我正在使用ApacheHTTP组件和NTCredentialsclass Credentials credentials=new NTCredentials(username, password, workstation, domain); AuthScope authScope=new AuthScope(AuthScope.ANY);

我正在使用Java对Sharepoint 2013进行rest api调用。如何使用jersey rest客户端连接到sharepoint 2013

注意:目前我正在使用ApacheHTTP组件和
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框架?

以下代码使用Jersey执行NTLM验证的HTTP GET请求:

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依赖项:

<dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>2.22.2</version>
</dependency>

org.glassfish.jersey.connectors
泽西apache连接器
2.22.2

请提供您试图创建解决方案的任何代码,提供证据证明您已经做了一些研究或了解了您试图实现的基本原则。人们将能够更多地帮助了解你正在尝试做什么,你尝试过什么,你读过什么。库和应用程序结构/框架的版本也会有所帮助。@DanielTung我已经添加了我的工作,我尝试了建议的解决方案,但如果我发布了以下形式的帖子:client.target(…).request().post(…)我的请求不会附加“Authorization”标题。我做错什么了吗?我刚刚发现,人们必须注意类路径上不同库的版本。目前,如果我依赖Tika(一个看似无关的库),而Tika恰好依赖于httpclient库,NTLM就会崩溃。在我的例子中,我怀疑“正确”的版本被gradles依赖解析替换掉了。