0

假设我有以下代码,它创建了一个 SSL 套接字google.com,并且我想查看 Google 的证书。我怎样才能得到一个实例javax.security.cert.X509Certificate[]

import java.io.*;
import java.math.*;
import java.net.*;
import java.security.*;

import javax.net.ssl.*;
import javax.net.*;


public class MWE{
    public static void main(String[] args) throws Exception{
        SSLContext sslContext = SSLContext.getDefault();
        SocketFactory clientSocketFactory = sslContext.getSocketFactory();

        String remoteHost = "google.com";
        int remotePort = 443;
        SSLSocket socket = null;
        try {
            //Lookup the "common name" field of the certificate from the remote server:
            socket = (SSLSocket) clientSocketFactory.createSocket(remoteHost, remotePort);
            socket.setEnabledCipherSuites(socket.getSupportedCipherSuites());
            socket.startHandshake();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }
}

我查看了所涉及的每个类的文档,但我还没有找到对包含证书的任何数据结构的任何引用。

4

1 回答 1

3
X509Certificate[] c = socket.getSession().getPeerCertificateChain();
于 2013-03-05T06:42:29.853 回答