2017-04-26 35 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? –

相關問題