httpclient 無信任證書直接使用https含項目demo

說下HTPPS:

 

HTTPS,即安全的超文本傳輸協議,採用了SSL技術,被普遍使用以保證Web應用系統的安全性。訪問Web應用的編程接口大多封裝了SSL,使得訪問HTTPS和訪問HTTP同樣簡單。可是不少中、小型應用系統或基於局域網、校園網的應用系統所使用的證書並非由權威的認證機構發行或者被其驗證,直接使用這些編程接口將不能訪問HTTPS。java

說下JSSE:apache

JSSE=SSL+TLS  本文便是使用了jsse輕鬆訪問HTPPS無信任證書請求..編程

Java安全套接擴展 (Java Secure Socket Extension, JSSE)是實現Internet安全通訊的一系列包的集合。它是一個SSL和TLS的純Java實現,能夠透明地提供數據加密、服務器認證、信息完整性等功能,可使咱們像使用普通的套接字同樣使用JSSE創建的安全套接字。JSSE是一個開放的標準,不僅是Sun公司才能實現一個JSSE,事實上其餘公司有本身實現的JSSE。
在深刻了解JSSE以前,須要瞭解一個有關Java安全的概念:客戶端的TrustStore文件。客戶端的TrustStore文件中保存着被客戶端所信任的服務器的證書信息。客戶端在進行SSL鏈接時,JSSE將根據這個文件中的證書決定是否信任服務器端的證書。

JSSE中,有一個信任管理器類負責決定是否信任遠端的證書,咱們重寫X509證書信任管理器後使其信任便可.在這就很少說了,能夠到這邊下載完整的DEMO.本身看看把.安全

轉:http://download.csdn.net/detail/q119909033/7930099服務器

原型:dom

post:socket

package com.zyf.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;


public class MySSLProtocolSocketFactory implements ProtocolSocketFactory {
	
	private SSLContext sslcontext = null; 
	
	private SSLContext createSSLContext() {   
	      SSLContext sslcontext=null;   
	      try {   
	          sslcontext = SSLContext.getInstance("SSL");   
	          sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());   
	      } catch (NoSuchAlgorithmException e) {   
	          e.printStackTrace();   
	      } catch (KeyManagementException e) {   
	          e.printStackTrace();   
	      }   
	      return sslcontext;   
	  }   
	   
	  private SSLContext getSSLContext() {   
	      if (this.sslcontext == null) {   
	          this.sslcontext = createSSLContext();   
	      }   
	      return this.sslcontext;   
	  }   
	   
	  public Socket createSocket(Socket socket, String host, int port, boolean autoClose)   
	          throws IOException, UnknownHostException {   
	      return getSSLContext().getSocketFactory().createSocket(   
	              socket,   
	              host,   
	              port,   
	              autoClose   
	          );   
	  }   
	  
	  public Socket createSocket(String host, int port) throws IOException,   
	          UnknownHostException {   
	      return getSSLContext().getSocketFactory().createSocket(   
	              host,   
	              port   
	          );   
	  }   
	   
	   
	  public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)   
	          throws IOException, UnknownHostException {   
	      return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);   
	  }   
	  
	  public Socket createSocket(String host, int port, InetAddress localAddress,   
	          int localPort, HttpConnectionParams params) throws IOException,   
	          UnknownHostException, ConnectTimeoutException {   
	      if (params == null) {   
	          throw new IllegalArgumentException("Parameters may not be null");   
	      }   
	      int timeout = params.getConnectionTimeout();   
	      SocketFactory socketfactory = getSSLContext().getSocketFactory();   
	      if (timeout == 0) {   
	          return socketfactory.createSocket(host, port, localAddress, localPort);   
	      } else {   
	          Socket socket = socketfactory.createSocket();   
	          SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);   
	          SocketAddress remoteaddr = new InetSocketAddress(host, port);   
	          socket.bind(localaddr);   
	          socket.connect(remoteaddr, timeout);   
	          return socket;   
	      }   
	  }   
	   
	  //自定義私有類   
	  private static class TrustAnyTrustManager implements X509TrustManager {   
	      
	      public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {   
	      }   
	  
	      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {   
	      }   
	  
	      public X509Certificate[] getAcceptedIssuers() {   
	          return new X509Certificate[]{};   
	      }   
	  }     

}
相關文章
相關標籤/搜索