Solved

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

Posted on 2013-01-17
3
3,971 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 98
What is the use of Forwarding Class in java 1 54
IntelliJ and Eclipse Neon 2 installations 4 69
Delete .class files at every compile 4 58
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

752 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