`
vtyi
  • 浏览: 84784 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

URLConnection连https报证书错误的解决方法

阅读更多

导入证书

在DOS窗口,切入证书路径下执行:
keytool -import -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit -keypass changeit -alias bocommca -file test_root.cer

"test_root.cer"为证书名.

 

删除证书

keytool -delete -alias bocommca -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit

 

 

URLConnection and https

with a java.net.URLConnection i can connect to any http server. it’s also possible to connect to an https server. if i connect to a https server with a browser i might get a message that the certificate is not trusted. i am prompted to examine the certificate and mark it as a trusted certificate. after that i can connect without any problems. the same must be done if i try to connect with an URLConnection. if we try to connect to an https server via URLConnection and the certificate is not trusted a javax.net.ssl.SSLHandshakeException is thrown with the message “PKIX path building failed”… at least for the sun jvm version 1.5.

add certificate to a KeyStore

first we need to download the certificate from the webserver. this can be done with firefox. if you accepted the servers certificate you can save the certificate by selecting: Edit->Preferences->Advanced->Encryption->View Certificates->Your Certificates here you need to select the certificate and then click on export. save it somewhere on your harddisk. with this certificate java cannot work directly… actually it can but it’s easier to transform it into a KeyStore file. with the command keytool -import -alias aliasOfCertifiate -file certificateFile.cer\ -keystore myKeystore the keytool program is distributed with a jdk. with the command we add the certificate certificateFile.cer as a trusted certificate to the keystore file named myKeystore. the tool prompts for a password. this password is used to encrypt the keystore file.
instead of adding the certificate to myKeystore we could also add it to the default keystore of the jvm. this is done with: keytool -import -alias aliasOfCertifiate -file certificateFile.cer\ -keystore $JAVA_HOME/lib/security/cacerts with the password “changeit”. this uses root privileges and it is the default setting of all java programs. it’s a bit like pollution of the “global” environment and it’s better to avoid this.

use that keystore

if i have an URLConnection with https as a protocol it’s an instance of HttpsURLConnection and i can simply cast to it. HttpsURLConnection has a method setSSLSocketFactory. this socketFactary can be configured to accept certain certificates or not. a socketFactory which accepts certificates in myKeystore can be created with the following code: InputStream in = new FileInputStream(new File("path/to/myKeystore")); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(in, "PasswordUsedWithKeytool".toCharArray()); in.close(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); X509TrustManager defaultTrustManager = (X509TrustManager)tmf.getTrustManagers()[0]; SSLContext context = SSLContext.getInstance(”TLS”); context.init(null, new TrustManager[] {defaultTrustManager}, null); SSLSocketFactory sslSocketFactory = context.getSocketFactory(); here the keystore is loaded at first. you have to provide the password you typed in during creation of the keystore file. after that a TrustManager is created via a TrustManagerFactory initialised with our KeyStore. then the SSLContext is created and initialised with the trustManager. after that a SSLSocketFactory can be created by the getSocketFactory method of the SSLContext. we can use it for our URLConnection like following: URL url = new URL("https://thesecuredomain.org"); URLConnection con = url.openConnection(); ((HttpsURLConnection) con).setSSLSocketFactory(sslSocketFactory); con.connect(); in = con.getInputStream(); ...
分享到:
评论

相关推荐

    [java程序设计与数据结构(第2版)]一书源码

    - Java的异常处理机制使得程序能优雅地处理错误,通过try-catch-finally语句块捕获和处理异常。 - 自定义异常:可以通过创建新的类继承自`Exception`或其子类来定义特定的异常类型。 5. **输入/输出流** - Java...

    简单浏览器 java

    8. **错误处理**:良好的错误处理机制能提高用户体验,例如,当请求失败或网络中断时,浏览器应该能够优雅地显示错误信息并提供解决方案。 9. **性能优化**:为了提供流畅的浏览体验,可能需要考虑缓存策略,比如...

    <面向对象程序设计---JAVA(第二版)>的示例程序

    - try-catch-finally:用于捕获和处理程序运行时可能出现的错误,保证程序的健壮性。 - 自定义异常:可以通过创建继承自Exception类的新类来定义特定类型的异常。 6. 集合框架: - List、Set、Map:Java集合框架...

    Thinking in Java第三版中文

    - 书中介绍了常见的设计模式,如工厂模式、单例模式、观察者模式等,这些都是解决软件设计问题的通用解决方案。 通过阅读《Thinking in Java》第三版,初学者不仅可以掌握Java编程的基本语法,还能深入理解面向...

    《Java程序设计实用教程(第3版)》例题

    - try-catch-finally:用于捕获和处理运行时错误,保证程序的健壮性。 - 自定义异常:可以创建自己的异常类,以便在特定情况下抛出异常。 4. 集合框架: - List、Set、Map接口:分别代表有序可重复、无序不重复...

    Thinking.In.Java

    - 异常是程序运行时发生的错误,Java提供了try-catch-finally结构来捕获和处理异常,保证程序的健壮性。 4. **集合框架** - Java集合框架包括List、Set、Map等接口及其实现类,如ArrayList、HashSet、HashMap等,...

    Java语言程序设计(源代码)

    通过分析源代码,可以了解如何有效利用这些类和方法解决问题。 通过研究这个压缩包中的源代码,读者可以逐步熟悉Java的语法和编程模式,提高编程技能,并能将所学应用到实际项目中。无论是初学者还是经验丰富的...

    java API文档中文版

    synchronized关键字和volatile变量提供了线程同步机制,保证了多线程环境下的数据一致性。 6. **异常处理**:Java的异常处理机制通过try-catch-finally语句块来捕获和处理运行时错误。Exception类是所有异常的基类...

    Deitel:通过 Deitel 和 Deitel 学习 Java - 示例

    4. **异常处理**:Java有强大的异常处理机制,通过try-catch-finally语句块来捕获和处理运行时错误,保证程序的健壮性。 5. **集合框架**:Java集合框架包括List、Set、Queue和Map等接口,以及ArrayList、...

Global site tag (gtag.js) - Google Analytics