Java에서 https 연결 확인

15674 단어 SSL자바HTTPS
https 연결 확인은 브라우저가 아닌 Java에서 수행합니다.
환경은 Java8을 사용합니다. 그 외에는 라이브러리는 이용하고 있지 않습니다.

출처


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("オペレーティングシステム名(os.name):");
            System.out.println(System.getProperty("os.name"));
            System.out.print("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("IPアドレス(getLocalHost):" + ip);
            System.out.println("ホスト名(hostName):" + hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static void dispCerts(HttpsURLConnection conn) {
        System.out.println("----サーバの証明書チェーンを表示----");
        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("----コンテンツを表示----");
        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();
        }
    }
}

실행 결과



일부 내용을 덮고있는 것과 생략을하고 있습니다.
オペレーティングシステム名(os.name):Windows 10
Javaバージョン(java.version):1.8.0_74
IPアドレス(getLocalHost):192.168.XXX.XXX
ホスト名(hostName):DESKTOP-XXXXX
----コンテンツを表示----
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head>
・・・
</body></html>
----サーバの証明書チェーンを表示----
[
[
  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
・・・

오류의 경우



기사를 쓸 때 참고로 한 사이트는 오류가 발생했습니다.
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

정상시와의 차이



분명히 기본 제한에 △가 붙어있는 것 같습니다.
알았던 적이 있으면 추기합니다.


실행 jar 파일



부담없이 실행할 수 있도록 jar 파일을 준비했습니다. 아래와 같은 jar 파일을 다운로드해, ​​움직이고 싶은 서버에 배치한 후에 java의 패스를 통과한 상태로 인수에 URL을 지정해 이용해 주세요.
htps : // 기주 b. 이 m / 여기 1212 / h tps 이런 ct / 등 w / 뭐 r / 그럼 r ぃぇ / h tps 이런 ct. 그럼

좋은 웹페이지 즐겨찾기