Java CertificateException “No subject alternative names matching IP address … found”

前端 未结 6 1710
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 10:39

I\'m trying to implement a selfsigned certificate into my webserver, and it\'s working already with firefox and chrome (both from the server itself and from a remote machine

6条回答
  •  灰色年华
    2020-12-31 11:06

    The reason why this fails is because the hostname of the target endpoint and the certificate common name (CN in certification Subject does not match).

    For e.g., from a JVM, when trying to connect to an IP address (WW.XX.YY.ZZ) and not the DNS name (https://stackoverflow.com), the HTTPS connection will fail because the certificate stored in the java truststore cacerts expects common name to match the target address.

    To mitigate this HostnameVerifier needs to be verify the connection despite the mismatch https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#HostnameVerifier

        HttpsURLConnection urlConnection = (HttpsURLConnection) new URL("https://test.test/api").openConnection();
        urlConnection.setSSLSocketFactory(buildSocketFactory());
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("get");
        urlConnection.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession sslSession) {
                return true;
            }
        });
        urlConnection.getOutputStream();
    

提交回复
热议问题