Java使用自簽證書訪問https網站筆記
用firefox瀏覽器取得https網站的server crt,並存成local.crt
1google firefox get ssl cert利用keytool產生java client端要用的client keysotre
12keytool -import -alias ocsg.keystore -keystore ocsg.keystore -file localhost.crt#建立的同時要輸入密碼,密碼會在下一步用到
- Java程式建立ttps connection時將ocsg.keystore讀入
java-https-client-code 12345678910111213141516171819202122232425262728293031public void test3() throws Exception {X509TrustManager sunJSSEX509TrustManager;// 加载 Keytool 生成的證書文件char[] passphrase;String p = "ocsgks"; //keystore 密碼passphrase = p.toCharArray();File file = new File("/ocsg.keystore"); //keystore檔案位置System.out.println("Loading KeyStore " + file + "...");InputStream in = new FileInputStream(file);KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());ks.load(in, passphrase);in.close();// 建立 javax.net.ssl.TrustManagerTrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");tmf.init(ks);TrustManager tms [] = tmf.getTrustManagers();// 使用TrustManager 訪問https網站SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tms, new java.security.SecureRandom());SSLSocketFactory ssf = sslContext.getSocketFactory();URL myURL = new URL("https://localhost:9191/ocsg-cht/"); //https網站urlHttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();httpsConn.setSSLSocketFactory(ssf);InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());int respInt = insr.read();while (respInt != -1) {System.out.print((char) respInt);respInt = insr.read();}}