简体   繁体   中英

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

I looked up the httpclient 4.3.3 APIs on how to specify the charset to be used for headers so the Authorization header containing the username/password can use a specific charset such as UTF-8 or iso-8859-1. The deprecated 3.x API used for this is

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

The equivalent API in 4.3.3, I found is in ConnectionConfig. Following is the code I tried using

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);

But the base64 encoded value of the credentials sent across in the authorization header indicates that the credentials are not encoded using the specified charset "iso-8859-1". Is ConnectionConfig.setCharset() the right method to use to set the http header charset? If not, what is the correct equivalent of the deprecated setHttpElementCharset() in 4.3.x?

Apache mail archive http://mail-archives.apache.org/mod_mbox/hc-dev/201407.mbox/%3CJIRA.12727350.1405435834469.45355.1405437005945@arcas%3E indicates this is not readily supported and suggests the use of BasicSchemeFactory but I can't seem to figure out how/where to specify the charset using that.

Custom charset encoding is applicable to some auth schemes (such Basic and Digest) and not applicable to others. Global custom auth charset parameter was a bad idea

Credentials charset has to be configured on a per scheme basis using custom auth scheme factories

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM