Solved

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

Posted on 2013-01-17
3
3,797 Views
Last Modified: 2013-01-18
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
0
Comment
Question by:West100
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 38789895
Ok, so the LINK that I posted in your other question should work pretty much AS-IS for your case (since you are also creating your DefaultHttpClient in code). Did you try it? You should just need to copy the class that they give code for, WebClientDevWrapper, and change line 32 in your code above to...
DefaultHttpClient httpclient = WebClientDevWrapper.wrapClient(new DefaultHttpClient());

Open in new window

0
 

Author Closing Comment

by:West100
ID: 38794559
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.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 38795297
Not a problem ;)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question