2 回答
TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
我認(rèn)為如何在調(diào)用任何 url 時(shí)提供 ntlm 身份驗(yàn)證的第一個(gè)答案?可以回答這個(gè)問(wèn)題。在 Java 8u201 中,有一個(gè)新的 JRE 選項(xiàng)jdk.http.ntlm.transparentAuth,默認(rèn)設(shè)置為禁用
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
我沒(méi)有找到 JRE 和 JDK 之間的區(qū)別。相反,我發(fā)現(xiàn)了這個(gè)解決方法。
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-win -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<version>4.5.7</version>
</dependency>
示例代碼
if (!WinHttpClients.isWinAuthAvailable()) {
log.warn("Integrated Win auth is not supported!!!");
}
// There is no need to provide user credentials
// HttpClient will attempt to access current user security context through
// Windows platform specific methods via JNI.
try (CloseableHttpClient httpclient = WinHttpClients.createDefault()) {
HttpGet httpget = new HttpGet(getRestUrl().toURI());
log.debug("Executing request " + httpget.getRequestLine());
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
int status = response .getStatusLine()
.getStatusCode();
if (status != 200) {
log.error("HTTP error " + status);
throw new RuntimeException("Failed : HTTP error code : " + status);
}
Type listType = new TypeToken<HashMap<String, App>>() {
}.getType();
return new Gson().fromJson(new InputStreamReader(response .getEntity()
.getContent(),
"UTF-8"), listType);
}
}
添加回答
舉報(bào)
