2 回答

TA贡献1880条经验 获得超4个赞
我认为如何在调用任何 url 时提供 ntlm 身份验证的第一个答案?可以回答这个问题。在 Java 8u201 中,有一个新的 JRE 选项jdk.http.ntlm.transparentAuth,默认设置为禁用

TA贡献1789条经验 获得超10个赞
我没有找到 JRE 和 JDK 之间的区别。相反,我发现了这个解决方法。
<!-- 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);
}
}
添加回答
举报