Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 SSL connection how-to!
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-05-09 20:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
If you have ever tried to connect to https by Java, chances are you will hit such exception:

Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

normal solution is to import cert w/ keytool, which drives lazybones like me mad.

but you can live w/o it. hereunder another easier solution:


package com.xxx.yyy.ssl;

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

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

import org.apache.log4j.Logger;

import com.sun.net.ssl.HostnameVerifier;
import com.sun.net.ssl.HttpsURLConnection;

public class SSlTrustManagerTool {
  protected static final Logger logger = Logger.getLogger(SSlTrustManagerTool.class);

  private SSlTrustManagerTool() {
  }

  /**
   * When reading the content from a HTTPS connection, a
   * <code>javax.net.ssl.SSLException:
   * untrusted server cert chain</code>
   * can be thrown for untrusted servers. To force reading from such untrusted
   * servers, this method installs a 'all-trustung' trust manager that returns
   * 'true' for all servers.
   * @throws NoSuchAlgorithmException
   * @throws KeyManagementException
   *
   * @throws Exception
   * if installation of the new trust manager failed.
   */
  public static void trustHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    // Create a trust manager that does not validate certificate chains:
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
      public X509Certificate[] getAcceptedIssuers() {
        logger.debug("getAcceptedIssuers");
        return null;
      }

      public void checkServerTrusted(X509Certificate[] certs,
          String authType) throws CertificateException {
        logger.debug("checkServerTrusted");
        return;
      }

      public void checkClientTrusted(X509Certificate[] certs,
          String authType) throws CertificateException {
        logger.debug("checkClientTrusted");
        return;
      }
    } // X509TrustManager
    };// TrustManager[]

    // Install the all-trusting trust manager:
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
      public boolean verify(String hostname, String session) {
        logger.debug(hostname + " " + session + " is accepted!");
        return true;
      }
    });
  }// trustHttpsCertificates()

}



With this class, you won't need to import any cert. Just run the static method before starting to connect any SSL server.

Note: The code is originally provided by somebody on Sun's forum. But the original code doesn't work for me. So I hacked it and finally get it work.



No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:SSL connection how-to! [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-05-09 20:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
here the link to the original post:

http://forum.java.sun.com/thread.jspa?threadID=693625&tstart=120

but it doesn't work at all.

at last, i found,

1. change javax.net.ssl.* to com.sun.net.ssl.*

2. no need to call setHostnameVerifier of your connection instance

and it works.


jigsaw edited on 2006-05-09 20:11

No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923