java - 使用 NTLM 对 Sharepoint 使用 HttpClient 身份验证机制时出现 HTTP 403 Forbidden

标签 java sharepoint proxy httpclient ntlm

HTTP/1.1 403 禁止访问 响应内容长度:729 结果:抛出“Microsoft.SharePoint.SoapServer.SoapServerException”类型的 soap:ServerException。此页面的安全验证无效。在 Web 浏览器中单击返回,刷新页面,然后重试您的操作。0x8102006d

我关心的是如何通过代理进行身份验证,同时在同一 session 中对 NTLM 配置的共享点进行身份验证?请帮助我。

提前致谢。

AuthCache authCache = new BasicAuthCache();
HttpHost targetHost = new HttpHost(<sharepointserverhostname>);
NTLMSchemeFactory f = new NTLMSchemeFactory();
HttpContext ctx = new BasicHttpContext();
AuthScheme ns = f.create(ctx);
authCache.put(targetHost, ns);

HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope(<proxyserverip>, 8080), new UsernamePasswordCredentials  ("testdomain\phanigandeed", "Jul@2014"));
credsProvider.setCredentials(
            AuthScope.ANY,  new NTCredentials("phanigandeed", "Jul@2014", "", "testdomain"));

CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

String listName = "PhaniList";
String description = "TestDescription";
String templateID = "101";
// for safety reasons, I had to remove the actual server details.
String endpointURL = <serviceurl>;  
String result = "Failed";
String username = "phanigandeed";
String psWord = "Jul@2014";
String domainName = "vsnl";
String XML_DATA = new String("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><AddList xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"><listName>"
                    + listName
                    + "</listName><description>"
                    + description
                    + "</description><templateID>"
                    + templateID
                    + "</templateID></AddList></soap:Body></soap:Envelope>");

HttpPost httpPost = new HttpPost(endpointURL);
httpPost.setHeader(new BasicHeader("Content-Type",
            "text/xml;charset=UTF-8"));
try {
StringEntity s = new StringEntity(XML_DATA, "UTF-8");
httpPost.setEntity(s);

System.out.println("executing request" + httpPost.getRequestLine());

HttpResponse response = httpclient.execute(httpPost, localContext);
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
    System.out.println("Response content length: "
                    + entity.getContentLength());
    result = EntityUtils.toString(entity);
    System.out.println("Result: " + result);
    entity.consumeContent();
}
System.out.println("result: " + result);

return;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Sharepoint Create Library failed :"
                + e.getMessage());

return;
}    

最佳答案

您需要连接到 SharePoint 并检索摘要以验证任何进一步的请求,这就是它抛出有关您的安全验证无效的异常的原因。

        String digest;
        try
        {
            string url = "https://Your.SharePoint.Site";
            HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
            client.BaseAddress = new System.Uri(url);
            string cmd = "_api/contextinfo";
            client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
            client.DefaultRequestHeaders.Add("ContentType", "application/json");
            client.DefaultRequestHeaders.Add("ContentLength", "0");
            StringContent httpContent = new StringContent("");
            var response = client.PostAsync(cmd, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                string content = response.Content.ReadAsStringAsync().Result;
                JsonObject val = JsonValue.Parse(content).GetObject();
                JsonObject d = val.GetNamedObject("d");
                JsonObject wi = d.GetNamedObject("GetContextWebInformation");
                digest = wi.GetNamedString("FormDigestValue");
            }
        }
        catch
        {
            MessageDialog dialog = new MessageDialog("Authentication to server failed.  Please try again.");
            dialog.ShowAsync();
        }

稍后您需要将摘要添加到下一个请求的 header 中以传递身份验证,如下所示。这是 C#,抱歉,我不知道 Java 特定的语法,但您需要 Digest 才能在所有平台上使用 Web 服务:

client.DefaultRequestHeaders.Add("X-RequestDigest", digest);

关于java - 使用 NTLM 对 Sharepoint 使用 HttpClient 身份验证机制时出现 HTTP 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379853/

相关文章:

java - 在获取 JSON 数据之前检查互联网连接 | waitFor() 返回 1

sharepoint - 新升级的 InfoPath 字段不在表单库中?

xml - XSLT 在输出中包含空元素

sharepoint - 以编程方式创建 MOSS 发布页面

go - 如何使用https和socks4代理

wcf - 在 WCF 服务中使用 WebClient

c# - 在 geckofx 中显示 html

java - 出现奇怪的 java.lang.NoClassDefFoundError 异常

java - 方法返回字符串的 Mockito 测试

java - 如何在登录后重定向处于不同 Activity 的 2 个不同用户?