Java Client Certificates for HTTPS/SSL Connections
In Java 6, creating HTTPS connections using client certificates involves a two-fold process: importing the server's self-signed root certificate into a truststore and specifying the client certificate and keystore information through system properties.
Importing the Server's Root Certificate
The self-signed root certificate must be imported into a truststore. This can be achieved using the command:
keytool -import -alias gridserver -file gridserver.crt -storepass $PASS -keystore gridserver.keystore
Specifying Client Certificate Information
To use the client certificate, the following system properties need to be set:
-Djavax.net.ssl.keyStoreType=pkcs12 -Djavax.net.ssl.trustStoreType=jks -Djavax.net.ssl.keyStore=clientcertificate.p12 -Djavax.net.ssl.trustStore=gridserver.keystore -Djavax.net.debug=ssl # for verbose debugging -Djavax.net.ssl.keyStorePassword=$PASS -Djavax.net.ssl.trustStorePassword=$PASS
Sample Code
The following code sample demonstrates using the client certificate for an HTTPS connection:
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); URL url = new URL("https://gridserver:3049/cgi-bin/ls.py"); HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); conn.setSSLSocketFactory(sslsocketfactory); InputStream inputstream = conn.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); String string = null; while ((string = bufferedreader.readLine()) != null) { System.out.println("Received " + string); }
By importing the server's root certificate into the truststore and specifying the necessary client certificate information, Java can successfully establish HTTPS connections with client certificates.
The above is the detailed content of How Do I Configure Java Client Certificates for Secure HTTPS Connections?. For more information, please follow other related articles on the PHP Chinese website!