【问题标题】:Allow java trust store to accept all SSL certificates?允许 java 信任库接受所有 SSL 证书吗?
【发布时间】:2012-10-13 20:04:47
【问题描述】:

访问this问题后。

我看到了一些答案,最好的显然是 so_mv 的答案。现在看来,他的答案现在已经过时了,因为我已经尝试过所有的导入和确切的代码,但它会产生大量错误。我查看了文档以查看最新的 java 中是否有任何更改,但我似乎无法找到原因。我认为对该问题的更新答案不仅对我有益,对整个社区也有益。

错误:

SecurityCheck.java:28: error: <identifier> expected
        sc.init(null, new TrustManager[] { trm }, null);
               ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                      ^
SecurityCheck.java:28: error: ')' expected
        sc.init(null, new TrustManager[] { trm }, null);
                         ^
SecurityCheck.java:28: error: not a statement
        sc.init(null, new TrustManager[] { trm }, null);
                                           ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                              ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                                                ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                 ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                                                      ^
SecurityCheck.java:28: error: <identifier> expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                       ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                        ^
SecurityCheck.java:29: error: illegal start of type
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                          ^
SecurityCheck.java:29: error: <identifier> expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                         ^
SecurityCheck.java:29: error: ';' expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                          ^
SecurityCheck.java:29: error: illegal start of type
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                           ^
SecurityCheck.java:29: error: <identifier> expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                            ^
SecurityCheck.java:29: error: ';' expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

【问题讨论】:

  • 它可能无法正常工作,因为您使用了错误的 Java 版本才能正常工作。您链接到的问题是使用com.sun.* 类,即strongly discouraged 正是出于这个原因,顺便说一句。至于如何解决,我不完全确定,抱歉。
  • 不过还是个好问题,所以 +1 :)
  • 这行代码主要有一个大问题:sc.init(null, new TrustManager[] { trm }, null);
  • 能否将错误粘贴到原问题中?
  • 啊,好吧。请记住,您可以使用其下方的share 链接获得答案的链接,这样您就可以直接链接到答案,以便我们知道您指的是哪个答案,否则我们可能会认为这是已接受的答案。

标签: java store trust


【解决方案1】:

查看您的错误后,我认为问题在于您已将代码直接放在class 块中,但它应该放在这样的方法中:

// package here
// imports here
public class SecurityCheck
{
    public void test() throws NoSuchAlgorithmException, KeyManagementException
                              // and any other exception here
    {
        // alternatively to throwing the exceptions to the caller,
        // you can handle them here using a try-catch-block

        // code from answer you linked to here
    }
}

我没有尝试过,我不知道这是否是唯一的问题,但它会解释你得到的错误。第一行是在class 主体中有效的变量声明和初始化,但sc.init(null, new TrustManager[] { trm }, null); 行不是(因为它是一个语句)并且需要在一个方法中。这也是错误从这一行开始的原因。

【讨论】:

  • 在我尝试这个之后我仍然有两个错误:SecurityCheck.java:28: error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown SSLContext sc = SSLContext.getInstance("SSL");SecurityCheck.java:29: error: unreported exception KeyManagementException; must be caught or declared to be thrown sc.init(null, new TrustManager[] { trm }, null);
  • 应该可以,但由于某种原因,我收到错误消息说它找不到符号“class NoSuchAlgorithmException”和“class KeyManagementException”,我觉得无法解决这个问题很愚蠢我自己的,我一定是错过了什么。
  • @Twisterz 我认为您需要将异常类添加到导入中,即import java.security.NoSuchAlgorithmExceptionimport java.security.KeyManagementException 或只是import java.security.* 以导入java.security 包中的所有类。
  • 是的,我刚刚想通了哈哈。就像我想的一样,我觉得自己很笨。
猜你喜欢
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多