簡體   English   中英

使用非ASCII憑據在httpclient 4.3.x中不起作用

[英]Use of non-ascii credentials not working in httpclient 4.3.x

我查找了httpclient 4.3.3 API,了解如何指定用於標頭的字符集,以便包含用戶名/密碼的Authorization標頭可以使用特定的字符集,例如UTF-8或iso-8859-1。 為此使用的不推薦使用的3.x API是

httpMethodInstance.getParams().setHttpElementCharset("iso-8859-1");

我發現4.3.3中的等效API在ConnectionConfig中。 以下是我嘗試使用的代碼

HttpClientBuilder builder = HttpClientBuilder.create();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));

builder.setDefaultCredentialsProvider(credentialsProvider);

ConnectionConfig connectionConfig = ConnectionConfig.custom()
        .setCharset(Charset.forName("iso-8859-1")).build();
BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(
        registryBuilder.build());
connManager.setConnectionConfig(connectionConfig);
builder.setConnectionManager(connManager);



HttpHost target = new HttpHost(host, port, scheme);
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

CloseableHttpClient httpclient = builder.build();
CloseableHttpResponse response = httpclient.execute(target, request, localContext);

但是,在授權標頭中發送的憑據的base64編碼值表示憑據未使用指定的字符集“ iso-8859-1”進行編碼。 ConnectionConfig.setCharset()是用於設置http標頭字符集的正確方法嗎? 如果不是,則4.3.x中已棄用的setHttpElementCharset()的正確等效項是什么?

Apache郵件存檔http://mail-archives.apache.org/mod_mbox/hc-dev/201407.mbox/%3CJIRA.12727350.1405435834469.45355.1405437005945@arcas%3E表示這不容易得到支持,並建議使用BasicSchemeFactory,但我可以似乎沒有弄清楚如何/在哪里指定字符集。

自定義字符集編碼適用於某些身份驗證方案(例如基本和摘要),而不適用於其他身份驗證方案。 全局自定義身份驗證字符集參數是個壞主意

必須使用自定義身份驗證方案工廠在每個方案的基礎上配置憑據字符集

Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.BASIC, new BasicSchemeFactory(Consts.UTF_8))
            .register(AuthSchemes.DIGEST, new DigestSchemeFactory(Consts.UTF_8))
            .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
            .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
            .build();
CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultAuthSchemeRegistry(authSchemeRegistry)
        .build();

暫無
暫無

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

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