Vérifiez la connexion https depuis Java, pas depuis le navigateur. L'environnement utilise Java 8. Aucune autre bibliothèque n'est utilisée.
package ssltest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
public class Connect {
public static void main(String[] args) {
try {
URL url = new URL("https://google.co.jp/");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.connect();
dispEnv();
dispContents(url);
dispCerts(conn);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void dispEnv() {
try {
System.out.print("Nom du système d'exploitation(os.name):");
System.out.println(System.getProperty("os.name"));
System.out.print("Version Java(java.version):");
System.out.println(System.getProperty("java.version"));
InetAddress ia = InetAddress.getLocalHost();
String ip = ia.getHostAddress();
String hostname = ia.getHostName();
System.out.println("adresse IP(getLocalHost):" + ip);
System.out.println("nom d'hôte(hostName):" + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static void dispCerts(HttpsURLConnection conn) {
System.out.println("----Afficher la chaîne de certificats du serveur----");
try {
Certificate[] certs = conn.getServerCertificates();
for (Certificate cert : certs) {
System.out.println(cert);
}
} catch (SSLPeerUnverifiedException e) {
e.printStackTrace();
}
}
public static void dispContents(URL url) {
System.out.println("----Afficher le contenu----");
try {
InputStream strm = url.openStream();
InputStreamReader in = new InputStreamReader(strm);
BufferedReader inb = new BufferedReader(in);
String line;
while ((line = inb.readLine()) != null) {
System.out.println(line);
}
inb.close();
in.close();
strm.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Une partie du contenu est masquée et omise.
Nom du système d'exploitation(os.name):Windows 10
Version Java(java.version):1.8.0_74
adresse IP(getLocalHost):192.168.XXX.XXX
nom d'hôte(hostName):DESKTOP-XXXXX
----Afficher le contenu----
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head>
・ ・ ・
</body></html>
----Afficher la chaîne de certificats du serveur----
[
[
Version: V3
Subject: CN=*.google.co.jp, O=Google LLC, L=Mountain View, ST=California, C=US
Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
・ ・ ・
Une erreur s'est produite sur le site auquel j'ai fait référence lors de la rédaction de l'article.
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at ssltest.Connect.main(Connect.java:22)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
... 11 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 17 more
Apparemment, la limite de base est marquée par △. Je l'ajouterai si je le découvre.
Nous avons préparé un fichier jar afin que vous puissiez l'exécuter facilement. Veuillez télécharger le fichier jar suivant, placez-le sur le serveur que vous souhaitez exécuter, puis spécifiez l'URL comme argument en passant par le chemin java. https://github.com/koni1212/httpsConnect/raw/master/jarfile/httpsConnect.jar
Recommended Posts