簡體   English   中英

Apache cxf java 客戶端 + ntlm 身份驗證和多用戶支持

[英]Apache cxf java client + ntlm authentication and multi user support

我正在使用 apache cxf java 客戶端連接我的 WS。 我也在使用 NTLM 進行身份驗證。

現在我面臨的問題是由於憑證緩存。 我第一次嘗試沒有權限訪問 WS 方法的用戶。 當我更改用戶時,它仍然使用相同的用戶訪問 WS 方法。

我在 tomcat 中運行,所以無法殺死我的 JVM .. 在 httpClientPolicy 上嘗試了所有可能的組合。

任何幫助將不勝感激。

這是 NTLM 特定的問題。 sun.net.www.protocol.https.HttpsURLConnectionImpl正在通過java.net.Authenticator. requestPasswordAuthentication()獲取 serverAuthorization java.net.Authenticator. requestPasswordAuthentication() java.net.Authenticator. requestPasswordAuthentication() 此授權信息保存在sun.net.www.protocol.http.AuthCacheValue.cache

所以如果我們覆蓋sun.net.www.protocol.http.AuthCacheValue意味着我們可以解決這個問題。

AuthCacheValue.setAuthCache(new AuthCache()
{
  @Override
  public void remove(String arg0, AuthCacheValue arg1) { }

  @Override
  public void put(String arg0, AuthCacheValue arg1) { }

  @Override
  public AuthCacheValue get(String arg0, String arg1)
  {
    return null;
  }
});

參考 :

http://web.archiveorange.com/archive/v/ACbGtycfTs2dqbRNpy6d

http://tigrou.nl/2011/06/11/cached-credentials-in-http-basic-authentication/

我用谷歌搜索並嘗試了很多解決這個問題的方法..顯然,最簡單的代碼是使用 JCIFS 庫的如下所示

    //Set the jcifs properties
    jcifs.Config.setProperty("jcifs.smb.client.domain", "domainname");
    jcifs.Config.setProperty("jcifs.netbios.wins", "xxx.xxx.xxx.xxx");
    jcifs.Config.setProperty("jcifs.smb.client.soTimeout", "300000"); // 5 minutes
    jcifs.Config.setProperty("jcifs.netbios.cachePolicy", "1200"); // 20 minutes
    jcifs.Config.setProperty("jcifs.smb.client.username", "username");
    jcifs.Config.setProperty("jcifs.smb.client.password", "password");

    //Register the jcifs URL handler to enable NTLM
    jcifs.Config.registerSmbURLHandler();

顯然,CXF 3.0 沒有使用 NTCredentials 實例配置 HTTP 客戶端 (4.3.x) 的有效方法。 請參考錯誤https://issues.apache.org/jira/browse/CXF-5671


順便說一句,如果您有一個需要傳輸的簡單消息,只需使用帶有 NTCredentials 實例的 HTTP 客戶端(我使用的是 4.3.4 .. 不確定早期版本)。 這對我來說也很神奇..示例如下:

    final NTCredentials ntCredentials = new NTCredentials("username", "Passworrd","destination", "domain");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    credsProvider.setCredentials(AuthScope.ANY, ntCredentials);
    CloseableHttpClient httpclient = HttpClientBuilder.create()
                                        .setDefaultCredentialsProvider(credsProvider)
                                        .build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM