因此,我尝试通过Java代码访问HTTPS服务器,但由于本地主机和服务器之间的SSL握手问题,导致无法访问。我尝试访问的服务器具有从私有证书颁发机构颁发的有效证书。
因此,在进行了一些研究之后,我将CA根证书导入了JVM信任库。我按如下所示使用keytool命令将证书导入到JRE。 keytool-导入-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(); }
}
错误:
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)
非常感谢您的帮助或帮助
答案 0 :(得分:0)
可能是因为您的信任库不在类路径中。 您应该执行以下操作:
Resource truststore = new ClassPathResource('your truststore name');