简体   繁体   中英

java - path to trustStore No trusted certificate found error

So I'm trying to access an HTTPS server from my Java code but I am not able due to an SSL Handshake issues between my localhost and the server. the Server that I am trying to access has valid certificate issued from Private Certificate Authority.

So after some research i imported the CA root certificate into the JVM trust store. I used keytool command as follows to import the certificate to JRE. keytool -import -alias mycertificate -keystore ..\\lib\\security\\cacerts -file c:\\mycert.cer

public static void main (String[]args)  {

try {

        // Open a secure connection.
        URL url = new URL("*****");

        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
        // Set up the connection properties
        con.setRequestProperty( "Connection", "close" );
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setConnectTimeout( 30000 );
        con.setReadTimeout( 30000 );

        con.setRequestProperty("Accept","application/json");

         con.setRequestProperty( "Content-Type","application/x-www-form- urlencoded");
        con.setRequestMethod("POST");




        // Set up the user authentication portion of the handshake with    the private




        File pKeyFile = new File("C:/cert.p12");
        String pKeyPassword = "xxxx";
        TrustManagerFactory tmf=     TrustManagerFactory.getInstance("SunX509");
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        InputStream keyInput = new FileInputStream(pKeyFile);
        keyStore.load(keyInput, pKeyPassword.toCharArray());
        keyInput.close();
        tmf.init(keyStore);


 SSLContext context = SSLContext.getInstance("SSL");

 context.init(null,  tmf.getTrustManagers(), new SecureRandom());

        SSLSocketFactory sockFact = context.getSocketFactory();
        con.setSSLSocketFactory( sockFact );


        // Send the request
        OutputStream outputStream = con.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(outputStream, "UTF-8");
        osw.write("grant_type=client_credentials&scope=sc0:fal");
        osw.flush();
        osw.close();

        // Check for errors
        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);


        InputStream inputStream;
        if (responseCode == HttpURLConnection.HTTP_OK) {
            //success
            inputStream = con.getInputStream();
        } else {
            inputStream = con.getErrorStream();
        }

        // Process the response
        BufferedReader reader;
        String line = null;
        reader = new BufferedReader( new InputStreamReader( inputStream ) );
        while( ( line = reader.readLine() ) != null )
        {
            System.out.println( line );
            //reader.append(line);
        }

        inputStream.close();
    } catch (Exception e) { e.printStackTrace(); }











    }

Error:

javax.net.ssl.SSLHandshakeException:      sun.security.validator.ValidatorException: No trusted certificate found
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)

Any help or assistance would be much appreciated, thank you

Probably it is because the your truststore is not on the classpath. You should do something like:

Resource truststore = new ClassPathResource('your truststore name');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM