2017-04-26 62 views
1

我正尝试使用http客户端使用ntlm认证方案从服务器下载pdf文件。没有提供有效的凭据(机制级别:没有提供有效的凭据(机制级别:未能找到任何Kerberos tgt))httpclient

但是我越来越低于错误时。当我使用wget输入用户名和密码作为参数时,该文件正在下载,但如果我使用相同的用户名和密码,则使用Java代码的401会失败。我正在使用httpclient 4.2.2

Authentication error: No valid credentials provided (Mechanism level: No valid credentials provided 
(Mechanism level: Failed to find any Kerberos tgt)) 

下面是我的代码,使用身份验证下载PDF。

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”是什么意思?有人有任何线索吗?

回答

0

下面的代码为我工作的HTTP客户端版本4.2.2。

DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpGet httpget = new HttpGet("url"); 
    CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(AuthScope.ANY, 
      new NTCredentials("username", "pwd", "", "domain")); 
       List<String> authtypes = new ArrayList<String>(); 
     authtypes.add(AuthPolicy.NTLM);  
     httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,authtypes); 

    localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider); 
    HttpResponse response = httpclient.execute(httpget, localContext); 
    HttpEntity entity=response.getEntity(); 
+0

NTCredentials构造函数的最后两个参数是什么意思:workstation和domani? –

相关问题