未提供有效的凭据(机制级别:未提供有效的凭据(机制级别:找不到任何Kerberos tgt))httpclient [英] No valid credentials provided (Mechanism level: No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt)) httpclient
问题描述
我正在尝试使用ntlm Auth Scheme从使用http客户端的服务器下载pdf文件.
I am trying to download pdf file from server using http client using ntlm Auth Scheme.
但是当我遇到错误时.当我使用带有用户名和密码作为参数的wget时,该文件将被下载,但是如果我使用相同的用户名和密码,则它将失败,并使用Java代码显示401.我正在使用httpclient 4.2.2
but I am getting below error when. The file is getting downloaded when I used wget with username and password as parameters but if I use same username and password it fails with 401 using java code. I am using httpclient 4.2.2
Authentication error: No valid credentials provided (Mechanism level: No valid credentials provided
(Mechanism level: Failed to find any Kerberos tgt))
下面是我使用auth下载pdf的代码.
Below is my code to download pdf using auth.
public ByteArrayOutputStream getFile1(String resourceURL) throws CRMBusinessException {
DefaultHttpClient httpclient = new DefaultHttpClient();
ByteArrayOutputStream tmpOut = null;
try {
ICRMConfigCache cache = CacheUtil.getCRMConfigCache();
String host = cache.getConfigValue(ConfigEnum.DOCUMENT_SOURCE_HOST_NAME.toString());
String user = cache.getConfigValue(ConfigEnum.HTTP_USER_NAME.toString());
String password = cache.getConfigValue(ConfigEnum.HTTP_PASSWORD.toString());
String workstation = cache.getConfigValue(ConfigEnum.CLIENT_HOST_NAME.toString());
// Prerequisites
PreCondition.checkEmptyString(resourceURL, "'resourceURL' cannot be empty or null");
PreCondition.checkEmptyString(host, ConfigEnum.DOCUMENT_SOURCE_HOST_NAME + " property is not set in database");
PreCondition.checkEmptyString(user, ConfigEnum.HTTP_USER_NAME + " property is not set in database");
PreCondition.checkEmptyString(password, ConfigEnum.HTTP_PASSWORD + " property is not set in database");
PreCondition.checkEmptyString(workstation, ConfigEnum.CLIENT_HOST_NAME + " property is not set in database");
// NTLM authentication across all hosts and ports
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_HOST),
new NTCredentials(user, password, workstation, MY_DOMAIN));
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
// Execute the GET request
HttpGet httpget = new HttpGet(resourceURL);
HttpResponse httpresponse = httpclient.execute(httpget);
if (httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
tmpOut = new ByteArrayOutputStream();
InputStream in = httpresponse.getEntity().getContent();
byte[] buf = new byte[1024];
int len;
while (true) {
len = in.read(buf);
if (len == -1) {
break;
}
tmpOut.write(buf, 0, len);
}
tmpOut.close();
}
aLog.debug( "IntranetFileDownloaderImpl - getFile - End - " + resourceURL);
return tmpOut;
} catch (Exception e) {
aLog.error("IntranetFileDownloaderImpl - getFile - Error while downloading " + resourceURL + "[" + e.getMessage() + "]", e);
throw new CRMBusinessException(e);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
在使用httpclient之前,有人遇到过此类问题吗? 无法找到任何Kerberos tgt"是什么意思? 有人有任何线索吗?
Has anyone faced this kind of issue before while using httpclient? What does "Failed to find any Kerberos tgt" mean? Anybody has any clue on it?
推荐答案
使用kotlin和httpclient版本4.5.8:
Using kotlin and httpclient version 4.5.8:
val credentialsProvider = BasicCredentialsProvider().apply {
setCredentials(
AuthScope(AuthScope.ANY),
NTCredentials(user, password, null, domain))
}
val requestConfig = RequestConfig.custom().setTargetPreferredAuthSchemes(listOf(AuthSchemes.NTLM)).build()
return HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
.build()
这篇关于未提供有效的凭据(机制级别:未提供有效的凭据(机制级别:找不到任何Kerberos tgt))httpclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!