gpt4 book ai didi

java - 使用 Apache HttpClient 和负载平衡的 SharePoint REST 调用在首次 GET 后失败

转载 作者:行者123 更新时间:2023-11-30 06:53:45 26 4
gpt4 key购买 nike

我偶然发现了 SharePoint REST 调用负载均衡器的问题。当我将 REST 调用直接发送到网络服务器时,一切似乎都工作正常。但是,当 REST 调用发送到负载均衡器时,只有第一个调用获得 HTTP 200 OK 状态,其余调用返回 HTTP 401 Unauthorized。经过一番研究我发现this article其中说:

这不是一个错误。 Windows 网络堆栈中内置了一个安全修复程序,可防止解析为环回地址的计算机名称接受连接,除非该计算机名称与 NETBIOS 名称匹配。

该文章中提到的唯一解决方法是更改​​服务器的配置,但这不是我们部门愿意做的事情。

目标是我们可以从 Java 应用程序执行 SharePoint REST 调用。

作为示例,我制作了一个小型测试程序,当我按下按钮时,它会向我的 SharePoint REST API 发出 GET 请求。

当我调整 Windows 主机文件以将负载平衡 url 映射到我们其中一台网络服务器的 IP 地址(在负载平衡器下)时,一切似乎都工作正常:

//first button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 200 OK

//second button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 200 OK

但是,一旦我们注释掉映射(并且请求发送到负载均衡器),问题就开始发生。

//first button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 200 OK

//second button click
Executing request GET https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('2') HTTP/1.1
[principal: domain.com\<username>] [workstation: <LBSERVER>]
----------------------------------------
HTTP/1.1 401 Unauthorized

如您所见,第一个 GET 请求运行良好。之后,我收到 HTTP 401 未经授权的错误。

即使身份验证数据位于 HttpContext 内部,为什么 HttpClient 会丢失其身份验证/ session (因为第一个请求运行良好)?我的客户端代码中是否缺少任何内容来解决此问题?

<小时/>

更新:

当在 get() 方法中调用 openClient() 和 closeClient() 时,两个 GET 请求都可以正常工作。但不需要重新创建我假设的 httpClient 吗?

<小时/>

以下是完整代码:

CloseableHttpClient httpClient;
CredentialsProvider credsProvider;
HttpClientContext context;
HttpHost targetHost;

Account account;

private void handleButtonAction(ActionEvent event) throws IOException {
get("https://lbserver.domain.com/sites/my_site/_api/web/Lists/getByTitle('Registratielijst')/Items('1')");
}

private void get(String uri) throws IOException {
HttpGet httpget = new HttpGet(uri);

System.out.println("\nExecuting request " + httpget.getRequestLine());

CloseableHttpResponse response = httpClient.execute(targetHost, httpget, context);
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());

response.close();
}

private void openClient() throws IOException {
System.out.println("Open client");

httpClient = HttpClients.createDefault();
targetHost = new HttpHost("lbserver.domain.com", 443, "https");
context = HttpClientContext.create();

credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost, AuthScope.ANY_REALM, "ntlm"),
new NTCredentials(account.getUsername(), account.getPassword(), "lbserver", "domain.com"));

context.setCredentialsProvider(credsProvider);

CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}

private void closeClient() throws IOException {
System.out.println("Close client");
if (httpClient != null) {
httpClient.close();
}
}

public void initialize(URL url, ResourceBundle rb) {
account = new Account();

try {
openClient();
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}

Runtime.getRuntime().addShutdownHook(new Thread(new Task() {
@Override
protected Object call() throws Exception {
closeClient();
return null;
}
}));
}

最佳答案

经过更多研究,我发现问题是由错误的 cookie 处理引起的。原因是 Apache HTTP 客户端使用 CookieSpecs.DEFAULT (RFC2109)。将其更改为 CookieSpecs.STANDARD (RFC6265) 后,它起作用了。

RequestConfig localConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.build();

httpClient = HttpClients.custom()
.setDefaultCookieStore(new BasicCookieStore())
.setDefaultCredentialsProvider(credsProvider)
.setDefaultRequestConfig(localConfig)
.build();

关于java - 使用 Apache HttpClient 和负载平衡的 SharePoint REST 调用在首次 GET 后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42208624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com