How to send NTLM authenticated post request using jersey?

前端 未结 1 1737
梦谈多话
梦谈多话 2021-01-05 02:09

I am making rest api calls to Sharepoint 2013 using Java. How can I connect to the sharepoint 2013 using jersey rest client?

Note: currently I am using apache http c

相关标签:
1条回答
  • 2021-01-05 02:44

    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;
    }
    

    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>
    
    0 讨论(0)
提交回复
热议问题