允许java信任存储接受所有SSL证书?

时间:2012-10-23 19:48:32

标签: java store trust

嗨,大家好,在访问这个问题之后:https://stackoverflow.com/a/5671038/1546070

我看到了几个答案,而最好的答案显然是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());

1 个答案:

答案 0 :(得分: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);不是(因为它是一个语句)并且需要在方法中。这也是错误从这一行开始的原因。