论坛首页 入门技术论坛

处理 https 工具

浏览 2152 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-21   最后修改:2010-01-26
package cn.finalist.estate.web.utils;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

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

import org.apache.log4j.Logger;

/**
* @author Squall
*
*/
public class TrustUtil {

   private static final Logger logger = Logger.getLogger(TrustUtil.class);
  
   /**
    * 获取验证信息 通过验证
    */
   public static void getTrust(){
     
      logger.info("getTrust function");
     
      X509TrustManager xtm = new X509TrustManager(){
//         X509Certificate[] chain;
//
//         public X509Certificate[] getAcceptedIssuers() {
//            throw new UnsupportedOperationException();
//         }
//
//         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//            throw new UnsupportedOperationException();
//         }
//
//         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//            this.chain = chain;
//            this.checkServerTrusted(chain, authType);
//         }
         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            System.out.println("检查客户端的可信任状态...");
         }
         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            System.out.println("检查服务器的可信任状态");
         }
         public X509Certificate[] getAcceptedIssuers() {
            System.out.println("获取接受的发行商数组...");
            return null;     
         }      
      };
     
      TrustManager mytm[] = {xtm};
      SSLContext sc = null;
     
      try {
         sc = SSLContext.getInstance("SSL");
      } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
      }
     
      try {
         sc.init(null,mytm,null);
      } catch (KeyManagementException e) {
         e.printStackTrace();
      }
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
   }
}
   发表时间:2010-12-20  
怎么调用?
请举一个 列子 好么??

0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics