Linux 初始化默认 SSL 上下文失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10063882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:40:20  来源:igfitidea点击:

Failure initializing default SSL context

javalinuxsslhttpshttpclient

提问by lujingsun

my httpclient project has encountered a strange problem .it run successful in other two Centos systems with java1.6. but it failed in another machine(centos and java1.6). the problem is:

我的httpclient项目遇到了一个奇怪的问题,在java1.6的另外两个Centos系统上运行成功。但它在另一台机器(centos 和 java1.6)上失败了。问题是:

java.lang.IllegalStateException: Failure initializing default SSL context
     [java]    at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:211)
     [java]    at org.apache.http.conn.ssl.SSLSocketFactory.(SSLSocketFactory.java:333)
     [java]    at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:165)
     [java]    at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:45)
     [java]    at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:294)
     [java]    at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:445)
     [java]    at simulativeLogin.WebClientDevWrapper.wrapClient(Unknown Source)
     [java]    at simulativeLogin.GetAccessToken.getToken(Unknown Source)
     [java]    at crawler.FriendshipCrawler.main(Unknown Source)
     [java]    at java.lang.reflect.Method.invoke(libgcj.so.10)
     [java]    at org.apache.tools.ant.taskdefs.ExecuteJava.run(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.taskdefs.Java.run(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.taskdefs.Java.executeJava(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.taskdefs.Java.executeJava(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.taskdefs.Java.execute(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.UnknownElement.execute(ant-1.7.1.jar.so)
     [java]    at java.lang.reflect.Method.invoke(libgcj.so.10)
     [java]    at org.apache.tools.ant.dispatch.DispatchUtils.execute(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Task.perform(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Target.execute(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Target.performTasks(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Project.executeSortedTargets(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Project.executeTarget(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Project.executeTargets(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Main.runBuild(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.Main.startAnt(ant-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.launch.Launcher.run(ant-launcher-1.7.1.jar.so)
     [java]    at org.apache.tools.ant.launch.Launcher.main(ant-launcher-1.7.1.jar.so)
     [java] Caused by: java.lang.IllegalStateException
     [java]    at gnu.javax.net.ssl.provider.X509KeyManagerFactory.engineGetKeyManagers(libgcj.so.10)
     [java]    at javax.net.ssl.KeyManagerFactory.getKeyManagers(libgcj.so.10)
     [java]    at org.apache.http.conn.ssl.SSLSocketFactory.createSSLContext(SSLSocketFactory.java:187)
     [java]    at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:209)

回答by Donal Fellows

I see that you're using GCJ as the implementation of Java (from the fact that the cause exception originates in the gnu.javax.net.ssl.provider.X509KeyManagerFactoryclass, and the way that many stack levels are in libgcj.so.10). Unfortunately, that's not a version of Java that's officially sanctioned; it has a number of issues in a few areas of its implementation and that can sometimes bite very hard. In particular, it seems that it has problems with X.509 key handling! (I didn't know that it did, but it clearly does.)

我看到您使用 GCJ 作为 Java 的实现(因为原因异常起源于gnu.javax.net.ssl.provider.X509KeyManagerFactory类,以及许多堆栈级别的方式libgcj.so.10)。不幸的是,这不是官方认可的 Java 版本。它在其实施的几个领域存在许多问题,有时会非常棘手。尤其是X.509密钥处理好像有问题!(我不知道它确实如此,但显然确实如此。)

The simplest fix is to switch to using a different Java implementation. The Sun/Oracle Java implementation is definitely good (I use it extensively) and I've heard good things about the OpenJDK implementation too; as I understand it, OpenJDK is the Sun JDK with some parts removed (notably the bits that they licensed from elsewhere, which I believeto be a chunk of the codec and GUI code, but I don't know for sure). What you don't want to do is to spend your time working around bugs in an implementation of Java that nobody recommends using!

最简单的解决方法是切换到使用不同的 Java 实现。Sun/Oracle Java 实现绝对是好的(我广泛使用它),而且我也听说过有关 OpenJDK 实现的好消息;据我了解,OpenJDK 是 Sun JDK,其中删除了一些部分(特别是他们从其他地方获得许可的部分,我认为这是编解码器和 GUI 代码的一部分,但我不确定)。您不想做的是将时间花在解决没有人推荐使用的 Java 实现中的错误上!

回答by lqu

It's not your fault. Use another JVM, that will solve your problem. I had the same problem. I'm running Apache HttpClient on Gumstix/linux/jamVM. I got the exactly the same exceptions. Here is a test program for X509. I ran it on my Mac mini, the result is "SunX509" as algorithm; but on jamVM, it is 'null'. Try it.

这不是你的错。使用另一个JVM,这将解决您的问题。我有同样的问题。我在 Gumstix/linux/jamVM 上运行 Apache HttpClient。我得到了完全相同的例外。这是X509的测试程序。我在我的 Mac mini 上运行它,结果是“SunX509”作为算法;但在 jamVM 上,它是 'null'。尝试一下。

public class TestX509 {
    public static void main(String[] args) {
        String algorithm = Security
            .getProperty("ssl.KeyManagerFactory.algorithm");

        System.out.println("algorithm is " + algorithm);
    }
}

See source of line 108 of gnu.javax.net.ssl.provider.X509KeyManagerFactory.java. This is where the exception comes from. (Because the 'current' is a key manager, which is null. You know that from the previous test.)

请参阅 gnu.javax.net.ssl.provider.X509KeyManagerFactory.java 第 108 行的源代码。这就是异常的来源。(因为“当前”是一个密钥管理器,它为空。您从之前的测试中知道这一点。)

104:   protected KeyManager[] engineGetKeyManagers()
105:   {
106:     if (current == null)
107:       {
108:         throw new IllegalStateException();
109:       }
110:     return new KeyManager[] { current };
111:   }
112:

Since the library throws IllegalStateException, which is a RuntimeException, some one considered it as a bug of GCJ and reported it here http://code.google.com/p/selenium/issues/detail?id=2483.

由于库抛出了 IllegalStateException,这是一个 RuntimeException,有人认为它是 GCJ 的一个错误,并在http://code.google.com/p/selenium/issues/detail?id=2483报告了它。

回答by Steve Owens

The answer is in the comments. If you do type java -versionand see java version "1.5.0", you will need to make sure that you are using Java 6. It should say java version "1.6.0"

答案就在评论里。如果您确实键入java -version并看到java version "1.5.0",则需要确保您使用的是 Java 6。它应该说java version "1.6.0"