0

I tried the recommendations in this post but have not been able to get a working solution.

Despite using TrustSelfSignedStrategy and NoopHostnameVerifier I am still getting the following error:

javax.net.ssl.SSLPeerUnverifiedException: Certificate for <fooHost> doesn't match any of the subject alternative names: []

It seems like if I could add fooHost to the SANs it would fix my issue but I am uncertain of how to do that.
This is my current implementation:

String serverHost = "fooHost"; 
String port = "1111";

TrustStrategy acceptingTrustStrategy = new TrustSelfSignedStrategy();       
SSLContext sslContext = 
    org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory sslsf = 
    new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
                
try( CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); ){
        String url = "https://" + serverHost+ ":" + port ;
        HttpPost totpPost = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add( new BasicNameValuePair( "param0", "value0") );
        params.add( new BasicNameValuePair( "param1", "value1") );
        totpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        
        try (CloseableHttpResponse totpResponse = httpClient.execute(totpPost)) {
            HttpEntity totpEntity = totpResponse.getEntity();
            String response = EntityUtils.toString(totpEntity);
            MyResponse nmr= objectMapper.readValue(response, MyResponse.class);
            EntityUtils.consume(totpEntity);
            return nmr;
        }
        
        
}catch(Exception e){
    log.error("Well this is fooBar:" , e);
}
RichardFeynman
  • 478
  • 1
  • 6
  • 16
  • 1
    Your issue seems similar to this one, please have a look here: https://stackoverflow.com/q/50928061/6777695 – Hakan54 Sep 16 '22 at 10:32
  • To anyone that finds this the above code works, the root issue was that my code was not being properly uploaded for testing. –  RichardFeynman Sep 23 '22 at 14:31

0 Answers0