有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

rest java到信任库的路径未找到可信证书错误

因此,我试图从Java代码访问HTTPS服务器,但由于本地主机和服务器之间的SSL握手问题,我无法访问。我试图访问的服务器具有从专用证书颁发机构颁发的有效证书

因此,经过一些研究之后,我将CA根证书导入到JVM信任存储中。我使用keytool命令将证书导入JRE,如下所示。 keytool-导入-别名mycertificate-密钥库。。\lib\security\cacerts-文件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(); }











    }

错误:

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)

任何帮助或帮助都将不胜感激,谢谢


共 (1) 个答案

  1. # 1 楼答案

    可能是因为您的信任库不在类路径上。 你应该这样做:

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