2014-01-24 148 views

回答

3

是的,可以配置Jersey Client通過需要NTLM身份驗證的代理服務器進行連接。

下面是一個簡單的代碼片段,準備合適的ClientConfig應與新澤西V2.5 +工作:

final ClientConfig config = new ClientConfig(); 

config.property(ClientProperties.PROXY_URI, "http://myproxy.com:8000"); 

CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 

final AuthScope ntlmAuthScope = 
     new AuthScope("myproxy.com", 8000, AuthScope.ANY_REALM, "NTLM"); 

credentialsProvider.setCredentials(
     ntlmAuthScope, 
     new NTCredentials("user", "password", "hostname", "domain")); 

config.property(
     ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider); 

config.connectorProvider(new ApacheConnectorProvider()); 

Client client = ClientBuilder.newClient(config); 

請注意:我使用與Jersey客戶端Apache的HttpClient的連接器 - 你可能需要略微不同的代碼,如果你使用another client transport connector

您可能還需要,如果你想你的POST/PUT請求,以響應來自代理服務器出來了407個認證挑戰緩衝(因此重複)以下行添加到您的代碼:

config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, 
       RequestEntityProcessing.BUFFERED); 
+0

我嘗試了建議的解決方案,但如果我這樣做了形式的一個帖子:。 client.target(...)請求()後(...) 沒有「授權」頭被安裝到我的請求。難道我做錯了什麼? – finrod

相關問題