JavaSheriff
JavaSheriff

Reputation: 7665

HttpClient have both SSL and Proxy authentication configured?

I have two pieces of code using HttpClient,
First part in case that the end point requires SSL
Second is proxy connection with basic authentication
My question Is how can I make this code conditional so in cases i have SSL + Proxy or SSL only I have hard time figuring out how to set the default credentials for example after I created the client using the client in the SSL part

.setDefaultCredentialsProvider(credsProvider)

This part is how I create the Client when I need SSL

CloseableHttpClient client = null;

    if(conf.isUseSslConfig()) {         
        SSLContext sslcontext = SSLContexts.custom()
                    .loadTrustMaterial(new File(conf.getTrustStoreLocation()), conf.getTrustStorePassword().toCharArray(), new TrustSelfSignedStrategy()).build();

            // Allow protocols
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,conf.getTlsVersions(), null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());               
            client = HttpClients.custom().setSSLSocketFactory(sslsf).build();


        }else {
            client= HttpClients.createDefault();                
        }

And this part is how I create the Client when I need Proxy authentication:

if(conf.isUseProxyConfig()){
    CredentialsProvider credsProvider = new BasicCredentialsProvider();        
    credsProvider.setCredentials(
            new AuthScope("fakeProxy.xerox.com", 80),
            new UsernamePasswordCredentials("xeroxUser","fakePassword123"));

HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
         }

So the bottom line is how to make the two sections work together so in case

  1. Call with SSL + Proxy and authentication
  2. Call with only SSL
  3. Call with only Proxy and authentication

Upvotes: 2

Views: 1009

Answers (1)

Hus Mukh
Hus Mukh

Reputation: 144

You can write code this way to get multiple conditions resolved :

CloseableHttpClient client = null;

    if(conf.isUseSslConfig() && conf.isUseProxyConfig()) {         

            setSSLSetting(client);
            setProxy()

        }else  if(conf.isUseSslConfig()) {

        setSSLSetting(client);
        }else {
            client= HttpClients.createDefault();                
        }


private void setProxy(){
    CredentialsProvider credsProvider = new BasicCredentialsProvider();        
    credsProvider.setCredentials(new AuthScope("fakeProxy.xerox.com", 80),new UsernamePasswordCredentials("xeroxUser","fakePassword123"));
}       


private void setSSLSetting(CloseableHttpClient client){
        SSLContext sslcontext = SSLContexts.custom()
                    .loadTrustMaterial(new File(conf.getTrustStoreLocation()), conf.getTrustStorePassword().toCharArray(), new TrustSelfSignedStrategy()).build();

            // Allow protocols
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,conf.getTlsVersions(), null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());               
            client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

or you can create methods that return client with different settings and configs like this :


    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();

    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

    private CloseableHttpClient createHttpClient(String headerName, String value) throws NoSuchAlgorithmException, KeyManagementException,KeyStoreException {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        Header header = new BasicHeader(headerName,value);
        List<Header> headers = new ArrayList<>();
        headers.add(header);
        RequestConfig reqConfig = RequestConfig.custom().setConnectionRequestTimeout(long milli seconds).build();

        CloseableHttpClient httpclient = HttpClients.custom().
                setDefaultHeaders(headers).
                setDefaultRequestConfig(reqConfig).
                setConnectionManager(cm).
                build();
        return httpclient;
    }

Upvotes: 1

Related Questions