如何使用球衣发送经过 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>
相关文章