【问题标题】:HTTP 403 Forbidden coming when using HttpClient authentication mechanism for Sharepoint using NTLM使用 NTLM 对 Sharepoint 使用 HttpClient 身份验证机制时出现 HTTP 403 禁止访问
【发布时间】:2014-12-10 08:54:18
【问题描述】:

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

我关心的是如何通过代理进行身份验证,同时在同一会话中对 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;
}    

【问题讨论】:

    标签: java sharepoint proxy httpclient ntlm


    【解决方案1】:

    您需要连接到 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();
            }
    

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

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

    【讨论】:

      【解决方案2】:

      HTTPClient 在使用 Sharepoint/IIS 和 NTLM 身份验证时遇到问题。当我切换到 Java HTTPURLConnection 时,我能够解决这些问题。请参阅以下帖子以供参考

      HTTP Client and NTLM issue

      HTTP Call to Sharepoint fails

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 2014-08-07
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 2023-02-24
        相关资源
        最近更新 更多