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

是否允许java信任存储接受所有SSL证书?,java,store,trust,Java,Store,Trust,参观后的问题 我看到了一些答案,而最好的答案显然是苏_mv的答案。现在看来他的答案已经过时了,因为我已经用所有的导入和精确的代码尝试过了,但是它产生了大量的错误。我查看了文档,看看在最新的java中是否有任何变化,但似乎找不到原因。我认为对这一问题的最新答案不仅对我有利,而且对整个社会都有利 错误: SecurityCheck.java:28: error: <identifier> expected sc.init(null, new TrustManager[]

参观后的问题

我看到了一些答案,而最好的答案显然是苏_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());
SecurityCheck.java:28:错误:应为
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:类型的非法开始
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:类型的非法开始
sc.init(null,新信任管理器[]{trm},null);
^
SecurityCheck.java:28:错误:“)”应为
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:不是语句
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:';'预期
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:类型的非法开始
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:';'预期
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:类型的非法开始
sc.init(null,新信任管理器[]{trm},null);
^
SecurityCheck.java:28:错误:应为
sc.init(null,新信任管理器[]{trm},null);
^
java:28:错误:';'预期
sc.init(null,新信任管理器[]{trm},null);
^
java:29:错误:类型的非法开始
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
^
java:29:错误:应为
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
^
java:29:错误:';'预期
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
^
java:29:错误:类型的非法开始
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
^
java:29:错误:应为
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
^
java:29:错误:';'预期
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

查看您的错误后,我认为问题在于您已将代码直接放入
块中,但应该将其放入如下方法中:

// 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
    }
}

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

这可能不起作用,因为您使用了错误的Java版本。你链接到的问题是使用一个
com.sun.*
类,正是出于这个原因,顺便说一句。至于如何修复它,我不完全确定,抱歉。这仍然是一个好问题,所以+1:)这行代码主要有一个大问题:
sc.init(null,new TrustManager[]{trm},null)共享
链接获得答案的链接,这样您就可以直接链接到答案,这样我们就可以知道您所指的答案,否则,我们可能会认为这是公认的答案。在我尝试了这一点之后,我仍然有两个错误:
SecurityCheck.java:28:error:unreported exception nosuchalgorithexception;必须捕获或声明为抛出SSLContext sc=SSLContext.getInstance(“SSL”)
SecurityCheck.java:29:错误:未报告的异常KeyManagementException;必须捕获或声明为抛出sc.init(null,new TrustManager[]{trm},null)
应该可以,但由于某些原因,我收到了一些错误,说它找不到符号“class NoSuchAlgorithmException”和“class KeyManagementException”,我觉得自己无法解决这个问题很愚蠢,我只是缺少了一些东西。@Twisterz我想你需要将异常类添加到导入中,也就是说,
import java.security.nosuchagorithmexception
import java.security.KeyManagementException
或者干脆
import java.security.
导入
java.security
包中的所有类。是的,我刚刚想出来了哈哈。我觉得自己很笨,就像我想的那样。