Unable to call the secure endpoint in Windows machine

Viewed 19

I have code to call the third party secure endpoint. This code is working fine in Linux environment with java Main class. Please find the below code how I set the certificates in to the java code.

import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

public class HttpPostRequest {

public static void demo() throws Exception {           
    String consumer_key = "a";
    String consumer_secret = "b";
    String access_token = "c";
    String access_secret= "d";

    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumer_key, consumer_secret);
    consumer.setTokenWithSecret(access_token, access_secret);


    String uri = "url";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(uri);
    
    System.out.println(httpPost.getURI().getHost());
    
    String json = "{ \"start\": { \"from\": 1615245718000, \"to\": 1620516118000 }}";
    StringEntity entity = new StringEntity(json);
    httpPost.setEntity(entity);
    
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    
    System.out.println(httpPost.getHeaders("Authorization"));
    System.out.println(httpPost.getHeaders("Accept"));
    
    try {
        consumer.sign(httpPost);
    }catch (Exception e) {
        e.printStackTrace();
    } 
    
    System.out.println(httpPost.getHeaders("Authorization"));
    System.out.println(httpPost.getHeaders("Accept"));
    
    HttpResponse response = httpclient.execute(httpPost);
    System.out.println(response.getStatusLine().toString());
    HttpEntity entity2 = response.getEntity();
    System.out.println();
    System.out.println(EntityUtils.toString(entity2));       
}
public static void main(String[] args) throws Exception {
    try {
        
        
        System.setProperty("javax.net.ssl.keyStoreType", "JKS");
        System.setProperty("javax.net.ssl.keyStore", "C:\\project\\myystore.jks");
        System.setProperty("javax.net.debug", "ssl # very verbose debug");
        System.setProperty("javax.net.ssl.keyStorePassword", "password1");
        
        System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0\\jre\\lib\\security\\cacerts");
        System.setProperty("javax.net.ssl.trustStorePassword", "password2");
        
        demo();
    }
    catch(IOException ioe) {
       ioe.printStackTrace();
    }
}
}

I tried with the same with the spring boot application in Windows and Linux environment and getting the same error.

I just copied the .jks file from the Linux server and gave the path in Windows environment.

I have two issues here.

  1. Why same code is not working in the Windows environment?
  2. How can I convert this to Spring boot application?

Error:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:450) at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:339)

0 Answers
Related