java https添加證書

添加證書:keytool -keystore "C:\Program Files\Java\jdkihu\jre\lib\security\cacerts" -importcert -alias ca12306 -file E:\srca12306\srca.cer
默認密碼:changeit

查看證書:keytool -list -alias ca12306 -keystore "C:\Program Files\Java\jdkihu\jre\lib\security\cacerts"

刪除證書:keytool -delete -alias ca12306 -keystore "C:\Program Files\Java\jdkihu\jre\lib\security\cacerts"

















































































java https訪問總結的代碼(不是都能用)

//	    /** 
//	     * 測試方法. 
//	     * @param args 
//	     * @throws Exception 
//	    */  
//	    public static void main(String[] args) throws Exception {  
//	        // 密碼  
//	        String password = "changeit";  
//	        // 密鑰庫  
//	        String keyStorePath = "C:\\Program Files\\Java\\jdkihu\\jre\\lib\\security\\cacerts";  
//	        // 信任庫  
//	        String trustStorePath = "C:\\Program Files\\Java\\jdkihu\\jre\\lib\\security\\cacerts";  
//	        // 本地起的https服務  
//	        String httpsUrl = spec;  
//	        // 傳輸文本  
//	        initHttpsURLConnection(password, keyStorePath, trustStorePath);  
//	        // 發起請求  
//	        get(httpsUrl);  
//	    }  
//	
//    /** 
//     * 初始化HttpsURLConnection. 
//     * @param password 
//     *            密碼 
//     * @param keyStorePath 
//     *            密鑰庫路徑 
//     * @param trustStorePath 
//     *            信任庫路徑 
//     * @throws Exception 
//     */  
//    public static void initHttpsURLConnection(String password,  
//            String keyStorePath, String trustStorePath) throws Exception {  
//        // 聲明SSL上下文  
//        SSLContext sslContext = null;  
//        // 實例化主機名驗證接口  
//        HostnameVerifier hnv = new MyHostnameVerifier();  
//        try {  
//            sslContext = getSSLContext(password, keyStorePath, trustStorePath);  
//        } catch (GeneralSecurityException e) {  
//            e.printStackTrace();  
//        }  
//        if (sslContext != null) {  
//            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext  
//                    .getSocketFactory());  
//        }  
//        HttpsURLConnection.setDefaultHostnameVerifier(hnv);  
//    }
//    
//    /** 
//     * 獲得SSLSocketFactory.
//     * @param password 
//     *            密碼 
//     * @param keyStorePath 
//     *            密鑰庫路徑 
//     * @param trustStorePath 
//     *            信任庫路徑 
//     * @return SSLSocketFactory 
//     * @throws Exception 
//     */
//    public static SSLContext getSSLContext(String password,  
//            String keyStorePath, String trustStorePath) throws Exception {  
//        // 實例化密鑰庫  
//        KeyManagerFactory keyManagerFactory = KeyManagerFactory  
//                .getInstance(KeyManagerFactory.getDefaultAlgorithm());  
//        // 獲得密鑰庫  
//        KeyStore keyStore = getKeyStore(password, keyStorePath);  
//        // 初始化密鑰工廠  
//        keyManagerFactory.init(keyStore, password.toCharArray());  
//  
//        // 實例化信任庫  
//        TrustManagerFactory trustManagerFactory = TrustManagerFactory  
//                .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
//        // 獲得信任庫  
//        KeyStore trustStore = getKeyStore(password, trustStorePath);  
//        // 初始化信任庫  
//        trustManagerFactory.init(trustStore);  
//        // 實例化SSL上下文  
//        SSLContext ctx = SSLContext.getInstance("TLS");  
//        // 初始化SSL上下文  
//        ctx.init(keyManagerFactory.getKeyManagers(),  
//                trustManagerFactory.getTrustManagers(), null);  
//        // 獲得SSLSocketFactory  
//        return ctx;  
//    }
//    /** 
//     * 獲得KeyStore. 
//     * @param keyStorePath 
//     *            密鑰庫路徑 
//     * @param password 
//     *            密碼 
//     * @return 密鑰庫 
//     * @throws Exception 
//     */
//    public static KeyStore getKeyStore(String password, String keyStorePath)  
//            throws Exception {  
//        // 實例化密鑰庫  
////        KeyStore ks = KeyStore.getInstance("JKS");  
//    	KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
//        // 獲得密鑰庫文件流  
//        FileInputStream is = new FileInputStream(keyStorePath);  
//        // 加載密鑰庫  
//        ks.load(is, password.toCharArray());  
//        // 關閉密鑰庫文件流  
//        is.close();  
//        return ks;  
//    }
//    
//    /** 
//     * 發送請求. 
//     * @param httpsUrl 
//     *            請求的地址 
//     * @param xmlStr 
//     *            請求的數據 
//     */
//    public static void get(String httpsUrl) {  
//        HttpsURLConnection urlCon = null;  
//        try {  
//            urlCon = (HttpsURLConnection) (new URL(httpsUrl)).openConnection();  
//            urlCon.setDoInput(true);  
//            urlCon.setDoOutput(true);  
//            urlCon.setRequestMethod("HEAD");         
//            System.out.println(urlCon.getResponseCode());
////            urlCon.setRequestMethod("POST");  
////            urlCon.setRequestProperty("Content-Length",  
////                    String.valueOf(xmlStr.getBytes().length));  
////            urlCon.setUseCaches(false);  
////            //設置爲gbk可以解決服務器接收時讀取的數據中文亂碼問題  
////            urlCon.getOutputStream().write(xmlStr.getBytes(charset));  
////            urlCon.getOutputStream().flush();  
////            urlCon.getOutputStream().close();  
////            BufferedReader in = new BufferedReader(new InputStreamReader(  
////                    urlCon.getInputStream()));  
////            String line;  
////            while ((line = in.readLine()) != null) {  
////                System.out.println(line);  
////            }  
//        } catch (MalformedURLException e) {  
//            e.printStackTrace();  
//        } catch (IOException e) {  
//            e.printStackTrace();  
//        } catch (Exception e) {  
//            e.printStackTrace();  
//        }  
//    } 
	
//	   public static void main(String[] args)
//	   {
//	        testIt();
//	   }
//	 
//	   private static void testIt(){
//	 
//	      String https_url = spec;
//	      URL url;
//	      try {
//	 
//		     url = new URL(https_url);
//		     HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
//	 
//		     //dumpl all cert info
//		     print_https_cert(con);
//	 
//		     //dump all the content
//		     print_content(con);
//	 
//	      } catch (MalformedURLException e) {
//		     e.printStackTrace();
//	      } catch (IOException e) {
//		     e.printStackTrace();
//	      }
//	 
//	   }
//	 
//	   private static void print_https_cert(HttpsURLConnection con){
//	 
//	    if(con!=null){
//	 
//	      try {
//	 
//		System.out.println("Response Code : " + con.getResponseCode());
//		System.out.println("Cipher Suite : " + con.getCipherSuite());
//		System.out.println("\n");
//	 
//		Certificate[] certs = con.getServerCertificates();
//		for(Certificate cert : certs){
//		   System.out.println("Cert Type : " + cert.getType());
//		   System.out.println("Cert Hash Code : " + cert.hashCode());
//		   System.out.println("Cert Public Key Algorithm : " 
//	                                    + cert.getPublicKey().getAlgorithm());
//		   System.out.println("Cert Public Key Format : " 
//	                                    + cert.getPublicKey().getFormat());
//		   System.out.println("\n");
//		}
//	 
//		} catch (SSLPeerUnverifiedException e) {
//			e.printStackTrace();
//		} catch (IOException e){
//			e.printStackTrace();
//		}
//	 
//	     }
//	 
//	   }
//	 
//	   private static void print_content(HttpsURLConnection con){
//		if(con!=null){
//	 
//		try {
//	 
//		   System.out.println("****** Content of the URL ********");			
//		   BufferedReader br = 
//			new BufferedReader(
//				new InputStreamReader(con.getInputStream()));
//	 
//		   String input;
//	 
//		   while ((input = br.readLine()) != null){
//		      System.out.println(input);
//		   }
//		   br.close();
//	 
//		} catch (IOException e) {
//		   e.printStackTrace();
//		}
//	 
//	       }
//	 
//	   }
//	public static void main(String[] args) throws Exception {
//	    URL url = new URL(spec);
//	    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(new Proxy(
//				Proxy.Type.HTTP,new InetSocketAddress(proxyAddress,proxyPort)
//				));
////	    SSLContext sc = SSLContext.getInstance("SSL");
////	    TrustManager[] tmArr={new X509TrustManager(){
////	        public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate,String paramString) throws CertificateException{}
////	        public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate,String paramString) throws CertificateException {}
////	        @Override
////	        public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;}
////			@Override
////			public void checkClientTrusted(
////					java.security.cert.X509Certificate[] arg0, String arg1)
////					throws java.security.cert.CertificateException {
////				// TODO Auto-generated method stub
////				
////			}
////			@Override
////			public void checkServerTrusted(
////					java.security.cert.X509Certificate[] arg0, String arg1)
////					throws java.security.cert.CertificateException {
////				// TODO Auto-generated method stub
////				
////			}
////	    }};
////	    sc.init(null, tmArr, new SecureRandom());
////	    conn.setSSLSocketFactory(sc.getSocketFactory());
////	    conn.connect();
//	    try{
//	    	BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//	        String input;
//	        while ((input = br.readLine()) != null)
//	            System.out.println(input);
//	    } catch(Exception e){
//	    	
//	    }
//	}
package com.ticket;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

/** 
 * 實現用於主機名驗證的基接口。  
 * 在握手期間,如果 URL 的主機名和服務器的標識主機名不匹配,則驗證機制可以回調此接口的實現程序來確定是否應該允許此連接。 
 */  
public class MyHostnameVerifier implements HostnameVerifier {  
    @Override  
    public boolean verify(String hostname, SSLSession session) {  
//        if("localhost".equals(hostname)){  
//            return true;  
//        } else {  
//            return false;  
//        }  
    	 return true;
    }  
} 


發佈了75 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章