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

12 篇文章 0 订阅

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

在Java中要访问Https链接时,会用到一个关键类HttpsURLConnection;参见如下实现代码:

  1.         // 创建URL对象
  2.         URL myURL = new URL("https://www.sun.com");
  3.         // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
  4.         HttpsURLConnection httpsConn = (HttpsURLConnection) myURL
  5.                 .openConnection();
  6.         // 取得该连接的输入流,以读取响应内容
  7.         InputStreamReader insr = new InputStreamReader(httpsConn
  8.                 .getInputStream());
  9.         // 读取服务器的响应内容并显示
  10.         int respInt = insr.read();
  11.         while (respInt != -1) {
  12.             System.out.print((char) respInt);
  13.             respInt = insr.read();
  14.         }

在取得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时的代码:

  1. package test;
  2. import java.io.FileInputStream;
  3. import java.security.KeyStore;
  4. import java.security.cert.CertificateException;
  5. import java.security.cert.X509Certificate;
  6. import javax.net.ssl.TrustManager;
  7. import javax.net.ssl.TrustManagerFactory;
  8. import javax.net.ssl.X509TrustManager;
  9. public class MyX509TrustManager implements X509TrustManager {
  10.     /*
  11.      * The default X509TrustManager returned by SunX509.  We'll delegate
  12.      * decisions to it, and fall back to the logic in this class if the
  13.      * default X509TrustManager doesn't trust it.
  14.      */
  15.     X509TrustManager sunJSSEX509TrustManager;
  16.     MyX509TrustManager() throws Exception {
  17.         // create a "default" JSSE X509TrustManager.
  18.         KeyStore ks = KeyStore.getInstance("JKS");
  19.         ks.load(new FileInputStream("trustedCerts"),
  20.             "passphrase".toCharArray());
  21.         TrustManagerFactory tmf =
  22.         TrustManagerFactory.getInstance("SunX509""SunJSSE");
  23.         tmf.init(ks);
  24.         TrustManager tms [] = tmf.getTrustManagers();
  25.         /*
  26.          * Iterate over the returned trustmanagers, look
  27.          * for an instance of X509TrustManager.  If found,
  28.          * use that as our "default" trust manager.
  29.          */
  30.         for (int i = 0; i < tms.length; i++) {
  31.             if (tms[i] instanceof X509TrustManager) {
  32.                 sunJSSEX509TrustManager = (X509TrustManager) tms[i];
  33.                 return;
  34.             }
  35.         }
  36.         /*
  37.          * Find some other way to initialize, or else we have to fail the
  38.          * constructor.
  39.          */
  40.         throw new Exception("Couldn't initialize");
  41.     }
  42.     /*
  43.      * Delegate to the default trust manager.
  44.      */
  45.     public void checkClientTrusted(X509Certificate[] chain, String authType)
  46.                 throws CertificateException {
  47.         try {
  48.             sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
  49.         } catch (CertificateException excep) {
  50.             // do any special handling here, or rethrow exception.
  51.         }
  52.     }
  53.     /*
  54.      * Delegate to the default trust manager.
  55.      */
  56.     public void checkServerTrusted(X509Certificate[] chain, String authType)
  57.                 throws CertificateException {
  58.         try {
  59.             sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
  60.         } catch (CertificateException excep) {
  61.             /*
  62.              * Possibly pop up a dialog box asking whether to trust the
  63.              * cert chain.
  64.              */
  65.         }
  66.     }
  67.     /*
  68.      * Merely pass this through.
  69.      */
  70.     public X509Certificate[] getAcceptedIssuers() {
  71.         return sunJSSEX509TrustManager.getAcceptedIssuers();
  72.     }
  73. }
  1.         // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  2.         TrustManager[] tm = { new MyX509TrustManager() };
  3.         SSLContext sslContext = SSLContext.getInstance("SSL""SunJSSE");
  4.         sslContext.init(null, tm, new java.security.SecureRandom());
  5.         // 从上述SSLContext对象中得到SSLSocketFactory对象
  6.         SSLSocketFactory ssf = sslContext.getSocketFactory();
  7.         // 创建URL对象
  8.         URL myURL = new URL("https://ebanks.gdb.com.cn/sperbank/perbankLogin.jsp");
  9.         // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
  10.         HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
  11.         httpsConn.setSSLSocketFactory(ssf);
  12.         // 取得该连接的输入流,以读取响应内容
  13.         InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
  14.         // 读取服务器的响应内容并显示
  15.         int respInt = insr.read();
  16.         while (respInt != -1) {
  17.             System.out.print((char) respInt);
  18.             respInt = insr.read();
  19.         }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值