Код: Выделить всё
C:\Program Files (x86)\Java\jdk1.8.0_25\jre\bin>keytool -import -keystore clienttrust.jks -file marta.cer -storepass storepass
Вот код:
Код: Выделить всё
public void testSimpleHttpsClient() throws CertificateException, InterruptedException, UnrecoverableKeyException, NoSuchAlgorithmException,
IOException, KeyManagementException, KeyStoreException {
URL url = new URL("https://intstg1-kaakioskpublicapi.ptstaging.ptec/TLE/36171/player/info");
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestMethod( "GET" );
SSLContext sslContext = SSLContext.getInstance("TLS");
char[] passphrase = "storepass".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(this.getClass().getResourceAsStream("clienttrust.jks"), passphrase);
System.out.println(ks);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
System.out.println(tmf.toString());
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
public boolean verify(String s, SSLSession sslSession) {
return s.equals(sslSession.getPeerHost());
}
};
con.setHostnameVerifier(hostnameVerifier);
sslContext.init(null, tmf.getTrustManagers(), null);
con.setSSLSocketFactory(sslContext.getSocketFactory());
int responseCode = con.getResponseCode();
InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK) {
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 );
}
inputStream.close();
}
Исключение в «основном» потоке javax. net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: доверенный сертификат не найден
в sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
в sun.security.ssl. SSLSocketImpl.fatal(SSLSocketImpl.java:1917)
на sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)
на sun.security.ssl.Handshaker.fatalSE(Handshaker.java: 295)
на sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1471)
на sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
на sun .security.ssl.Handshaker.processLoop(Handshaker.java:936)
на sun.security.ssl.Handshaker.process_record(Handshaker.java:871)
на sun.security.ssl.SSLSocketImpl.readRecord (SSLSocketImpl.java:1043)
на sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
на sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371) на sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
на sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
на sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
на sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
на sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
на java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
на sun.net.www. протокол.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
в httpConnection.Connection.testSimpleHttpsClient(Connection.java:179)
в httpConnection.Connection.main(Connection.java:22)
в sun.reflect.NativeMethodAccessorImpl.invoke0(собственный метод)
в sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
в sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43)
в java.lang.reflect.Method.invoke(Method.java:483)
в com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Вызвано: sun.security.validator.ValidatorException: доверенный сертификат не найден
в sun.security.validator.SimpleValidator.buildTrustedChain(SimpleValidator.java:384)
в sun.security.validator.SimpleValidator .engineValidate(SimpleValidator.java:133)
на sun.security.validator.Validator.validate(Validator.java:260)
на sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) )
на sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
на sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
на sun. Security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1453)
... еще 20
Ребята, объясните мне, пожалуйста, как решить эту проблему.
Подробнее здесь: https://stackoverflow.com/questions/269 ... curity-val