Link to home
Start Free TrialLog in
Avatar of West100
West100

asked on

Spring REST Call Problem: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

We could use some help resolving a problem calling/consuming a RESTful web service from a https URL via Springs REST Template. Previous to a recent change of the URL from http to https we have been pulling content via Spring's RestTemplate without any problems.

The following outlines how an instance of Resttemplate is being created before making the following successful call: restTemplate.getForObject(url, String.class) to a http URL :

 
  /**
      * Get Rest Template
      * @return
      */
      public RestTemplate getRestTemplate() {
                
        ContextAwareHttpComponentsClientHttpRequestFactory customRequestFactory = getRequestFactory();
        RestTemplate restTemplate = new RestTemplate(customRequestFactory);
        return restTemplate;

      }
    
    /**
      * Set Rest Template Request Factory
      * @return
      */
      public void setRestTemplateRequestFactory( ) {
          
        ContextAwareHttpComponentsClientHttpRequestFactory customRequestFactory = getRequestFactory();
        this.restTemplate.setRequestFactory(customRequestFactory);
   
      }
    
    /**
      * Get Request Factory
      * @return
      */

      private ContextAwareHttpComponentsClientHttpRequestFactory getRequestFactory() {
          
          HttpHost targetHost = new HttpHost(Service_URL_Base());             
      DefaultHttpClient httpclient = new DefaultHttpClient();
      httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(API_USERNAME, API_PASSWORD));
      
      // Create AuthCache instance
      AuthCache authCache = new BasicAuthCache();

      // Generate BASIC scheme object and add it to the local auth cache
      BasicScheme basicAuth = new BasicScheme();
      authCache.put(targetHost, basicAuth);

      // Add AuthCache to the execution context
      BasicHttpContext localcontext = new BasicHttpContext();
      localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
      ContextAwareHttpComponentsClientHttpRequestFactory customFactory = new ContextAwareHttpComponentsClientHttpRequestFactory( httpclient);
      customFactory.setHttpContext(localcontext);

      return customFactory;

      }
   
    /**
      * closeConnection
      * @return
      */
    private void closeConnection(RestTemplate restTemplate) {

      ((ContextAwareHttpComponentsClientHttpRequestFactory) restTemplate
      .getRequestFactory()).getHttpClient().getConnectionManager()
      .shutdown();

      }

Open in new window



After the URL change from http to https we are now experiencing the following error:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for THE https:// site HERE :peer not authenticated; nested exception is javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

Open in new window



We do not care if a certificate is valid or not.  Hopefully that might simplify a solution. We are not sure how to incorporate code into the RestTemplate scenario that will
help avoid the javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated error?

Does anyone have any background with any of this, with possible working example?  We have not been able to confidently find an example of something that will work for us with some explainationof where and how to implement in our Spring Rest template scenario.  Again disabling that SSL check is something that should work for us.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of West100
West100

ASKER

Yes, the Link you posted is just what we needed. We created a class that basically is the WebClientDevWrapper class and called it from our code and everything looks good seems to be running as expected.

Thanks for the help.
Not a problem ;)