转:Java中用HttpsURLConnection访问Https链接的问题

在web应用交互过程中,有很多场景需要保证通信数据的安全;在前面也有好多篇文章介绍了在Web Service调用过程中用WS-Security来保证接口交互过程的安全性,值得注意的是,该种方式基于的传输协议仍然是Http,采用这种方式可扩 展性和数据交互效率比较高;另外一种实现方式就是用Https,他是在协议层对Http的再次封装,加入了SSL/TLS,采用该协议进行通信的数据全部 都会被加密,由于目前Web开发编程中对此都有了一定程度的封装,所以采用Https对外提供服务,除了证书以外,对编程能力的要求并不高,相对于前者门 槛较低,但是由于对双方通信的所有数据都进行加密,而且交互过程中还有多次握手等,所以效率较低;以下就介绍下在Java中访问Https链接时会出现的 一些问题;

在Java中要访问Https链接时,会用到一个关键类HttpsURLConnection;参见如下实现代码:
Java代码 复制代码  收藏代码
  1. // 创建URL对象   
  2. URL myURL = new URL("https://www.sun.com");   
  3.   
  4. // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象   
  5. HttpsURLConnection httpsConn = (HttpsURLConnection) myURL   
  6.         .openConnection();   
  7.   
  8. // 取得该连接的输入流,以读取响应内容   
  9. InputStreamReader insr = new InputStreamReader(httpsConn   
  10.         .getInputStream());   
  11.   
  12. // 读取服务器的响应内容并显示   
  13. int respInt = insr.read();   
  14. while (respInt != -1) {   
  15.     System.out.print((char) respInt);   
  16.     respInt = insr.read();   
  17. }  
           // 创建URL对象
           URL myURL = new URL("https://www.sun.com");

           // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
           HttpsURLConnection httpsConn = (HttpsURLConnection) myURL
                   .openConnection();

           // 取得该连接的输入流,以读取响应内容
           InputStreamReader insr = new InputStreamReader(httpsConn
                   .getInputStream());

           // 读取服务器的响应内容并显示
           int respInt = insr.read();
           while (respInt != -1) {
               System.out.print((char) respInt);
               respInt = insr.read();
           }


在取得connection的时候和正常浏览器访问一样,仍然会验证服务端的证书是否被信任(权威机构发行或者被权威机构签名);如果服务端证书不被信任,则默认的实现就会有问题,一般来说,用SunJSSE会抛如下异常信息:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

上面提到SunJSSE,JSSE(Java Secure Socket Extension)是实现Internet安全通信的一系列包的集合。它是一个SSL和TLS的纯Java实现,可以透明地提供数据加密、服务器认证、 信息完整性等功能,可以使我们像使用普通的套接字一样使用JSSE建立的安全套接字。JSSE是一个开放的标准,不只是Sun公司才能实现一个 SunJSSE,事实上其他公司有自己实现的JSSE,然后通过JCA就可以在JVM中使用。
关于JSSE的详细信息参考官网Reference:http://java.sun.com/j2se/1.5.0/docs/guide/security/jsse/JSSERefGuide.html;
以及Java Security Guide:http://java.sun.com/j2se/1.5.0/docs/guide/security/;

在深入了解JSSE之前,需要了解一个有关Java安全的概念:客户端的TrustStore文件。客户端的TrustStore文件中保存着被客 户端所信任的服务器的证书信息。客户端在进行SSL连接时,JSSE将根据这个文件中的证书决定是否信任服务器端的证书。在SunJSSE中,有一个信任 管理器类负责决定是否信任远端的证书,这个类有如下的处理规则:
1、若系统属性javax.net.sll.trustStore指定了TrustStore文件,那么信任管理器就去jre安装路径下的lib/security/目录中寻找并使用这个文件来检查证书。
2、若该系统属性没有指定TrustStore文件,它就会去jre安装路径下寻找默认的TrustStore文件,这个文件的相对路径为:lib/security/jssecacerts。
3、若jssecacerts不存在,但是cacerts存在(它随J2SDK一起发行,含有数量有限的可信任的基本证书),那么这个默认的TrustStore文件就是lib/security/cacerts。

那遇到这种情况,怎么处理呢?有以下两种方案:
1、按照以上信任管理器的规则,将服务端的公钥导入到jssecacerts,或者是在系统属性中设置要加载的trustStore文件的路径;证书导入可以用如下命令:keytool -import -file src_cer_file –keystore dest_cer_store;至于证书可以通过浏览器导出获得;
2、实现自己的证书信任管理器类,比如MyX509TrustManager,该类必须实现X509TrustManager接口中的三个method;然后在HttpsURLConnection中加载自定义的类,可以参见如下两个代码片段,其一为自定义证书信任管理器,其二为connect时的代码:
Java代码 复制代码  收藏代码
  1.    package test;   
  2.   
  3.    import java.io.FileInputStream;   
  4.    import java.security.KeyStore;   
  5.    import java.security.cert.CertificateException;   
  6.    import java.security.cert.X509Certificate;   
  7.   
  8.    import javax.net.ssl.TrustManager;   
  9.    import javax.net.ssl.TrustManagerFactory;   
  10.    import javax.net.ssl.X509TrustManager;   
  11.   
  12.    public class MyX509TrustManager implements X509TrustManager {   
  13.   
  14.        /*  
  15.         * The default X509TrustManager returned by SunX509. We'll delegate  
  16.         * decisions to it, and fall back to the logic in this class if the  
  17.         * default X509TrustManager doesn't trust it.  
  18.         */  
  19.        X509TrustManager sunJSSEX509TrustManager;   
  20.   
  21.        MyX509TrustManager() throws Exception {   
  22.            // create a "default" JSSE X509TrustManager.   
  23.   
  24.            KeyStore ks = KeyStore.getInstance("JKS");   
  25.            ks.load(new FileInputStream("trustedCerts"),   
  26.                "passphrase".toCharArray());   
  27.   
  28.            TrustManagerFactory tmf =   
  29.            TrustManagerFactory.getInstance("SunX509""SunJSSE");   
  30.   
  31.             tmf.init(ks);   
  32.   
  33.          TrustManager tms [] = tmf.getTrustManagers();   
  34.   
  35.          /*  
  36.           * Iterate over the returned trustmanagers, look  
  37.           * for an instance of X509TrustManager. If found,  
  38.           * use that as our "default" trust manager.  
  39.           */  
  40.          for (int i = 0; i < tms.length; i++) {   
  41.              if (tms[i] instanceof X509TrustManager) {   
  42.                  sunJSSEX509TrustManager = (X509TrustManager) tms[i];   
  43.                  return;   
  44.              }   
  45.          }   
  46.   
  47.          /*  
  48.           * Find some other way to initialize, or else we have to fail the  
  49.           * constructor.  
  50.           */  
  51.          throw new Exception("Couldn't initialize");   
  52.      }   
  53.   
  54.      /*  
  55.       * Delegate to the default trust manager.  
  56.       */  
  57.      public void checkClientTrusted(X509Certificate[] chain, String authType)   
  58.                  throws CertificateException {   
  59.          try {   
  60.              sunJSSEX509TrustManager.checkClientTrusted(chain, authType);   
  61.          } catch (CertificateException excep) {   
  62.              // do any special handling here, or rethrow exception.   
  63.          }   
  64.      }   
  65.   
  66.      /*  
  67.       * Delegate to the default trust manager.  
  68.       */  
  69.      public void checkServerTrusted(X509Certificate[] chain, String authType)   
  70.                  throws CertificateException {   
  71.          try {   
  72.              sunJSSEX509TrustManager.checkServerTrusted(chain, authType);   
  73.          } catch (CertificateException excep) {   
  74.              /*  
  75.               * Possibly pop up a dialog box asking whether to trust the  
  76.               * cert chain.  
  77.               */  
  78.          }   
  79.      }   
  80.   
  81.      /*  
  82.       * Merely pass this through.  
  83.       */  
  84.      public X509Certificate[] getAcceptedIssuers() {   
  85.          return sunJSSEX509TrustManager.getAcceptedIssuers();   
  86.      }   
  87. }   
  88.   
  89.           // 创建SSLContext对象,并使用我们指定的信任管理器初始化   
  90.          TrustManager[] tm = { new MyX509TrustManager() };   
  91.          SSLContext sslContext = SSLContext.getInstance("SSL""SunJSSE");   
  92.   
  93.          sslContext.init(null, tm, new java.security.SecureRandom());   
  94.   
  95.          // 从上述SSLContext对象中得到SSLSocketFactory对象   
  96.          SSLSocketFactory ssf = sslContext.getSocketFactory();   
  97.   
  98.          // 创建URL对象   
  99.          URL myURL = new URL("https://ebanks.gdb.com.cn/sperbank/perbankLogin.jsp");   
  100.   
  101.          // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象   
  102.          HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();   
  103.          httpsConn.setSSLSocketFactory(ssf);   
  104.   
  105.          // 取得该连接的输入流,以读取响应内容   
  106.          InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());   
  107.   
  108.          // 读取服务器的响应内容并显示   
  109.          int respInt = insr.read();   
  110.          while (respInt != -1) {   
  111.              System.out.print((char) respInt);   
  112.              respInt = insr.read();   
  113.          }  
   package test;

   import java.io.FileInputStream;
   import java.security.KeyStore;
   import java.security.cert.CertificateException;
   import java.security.cert.X509Certificate;

   import javax.net.ssl.TrustManager;
   import javax.net.ssl.TrustManagerFactory;
   import javax.net.ssl.X509TrustManager;

   public class MyX509TrustManager implements X509TrustManager {

       /*
        * The default X509TrustManager returned by SunX509. We'll delegate
        * decisions to it, and fall back to the logic in this class if the
        * default X509TrustManager doesn't trust it.
        */
       X509TrustManager sunJSSEX509TrustManager;

       MyX509TrustManager() throws Exception {
           // create a "default" JSSE X509TrustManager.

           KeyStore ks = KeyStore.getInstance("JKS");
           ks.load(new FileInputStream("trustedCerts"),
               "passphrase".toCharArray());

           TrustManagerFactory tmf =
           TrustManagerFactory.getInstance("SunX509", "SunJSSE");

            tmf.init(ks);

         TrustManager tms [] = tmf.getTrustManagers();

         /*
          * Iterate over the returned trustmanagers, look
          * for an instance of X509TrustManager. If found,
          * use that as our "default" trust manager.
          */
         for (int i = 0; i < tms.length; i++) {
             if (tms[i] instanceof X509TrustManager) {
                 sunJSSEX509TrustManager = (X509TrustManager) tms[i];
                 return;
             }
         }

         /*
          * Find some other way to initialize, or else we have to fail the
          * constructor.
          */
         throw new Exception("Couldn't initialize");
     }

     /*
      * Delegate to the default trust manager.
      */
     public void checkClientTrusted(X509Certificate[] chain, String authType)
                 throws CertificateException {
         try {
             sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
         } catch (CertificateException excep) {
             // do any special handling here, or rethrow exception.
         }
     }

     /*
      * Delegate to the default trust manager.
      */
     public void checkServerTrusted(X509Certificate[] chain, String authType)
                 throws CertificateException {
         try {
             sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
         } catch (CertificateException excep) {
             /*
              * Possibly pop up a dialog box asking whether to trust the
              * cert chain.
              */
         }
     }

     /*
      * Merely pass this through.
      */
     public X509Certificate[] getAcceptedIssuers() {
         return sunJSSEX509TrustManager.getAcceptedIssuers();
     }
}

          // 创建SSLContext对象,并使用我们指定的信任管理器初始化
         TrustManager[] tm = { new MyX509TrustManager() };
         SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");

         sslContext.init(null, tm, new java.security.SecureRandom());

         // 从上述SSLContext对象中得到SSLSocketFactory对象
         SSLSocketFactory ssf = sslContext.getSocketFactory();

         // 创建URL对象
         URL myURL = new URL("https://ebanks.gdb.com.cn/sperbank/perbankLogin.jsp");

         // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
         HttpsURLConnection 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();
         }


对于以上两种实现方式,各有各的优点,第一种方式不会破坏JSSE的安全性,但是要手工导入证书,如果服务器很多,那每台服务器的JRE都必须做相同的操作;第二种方式灵活性更高,但是要小心实现,否则可能会留下安全隐患;

转自:http://hi.baidu.com/wzbg/blog/item/258a59cb81da5c13be09e68e.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值