是否有可能让 Java 忽略“信任存储”? 并接受它获得的任何 SSL 证书?

发布于 2024-07-29 16:48:07 字数 234 浏览 12 评论 0原文

我正在尝试编写一个使用 javax.mail API 发送邮件的 SSL 客户端。 我遇到的问题是服务器请求我使用 SSL,但服务器还配置了非标准 SSL 证书。 我发现的网页说我需要将证书安装到信任存储中。 我不想这样做(我没有必要的权限。)

  1. 有没有办法让 Java 忽略证书错误并接受它?
  2. 如果做不到这一点,有没有办法让信任存储位于我的程序本地,而不是为整个 JVM 安装?

I am trying to write an SSL client that sends mail using the javax.mail API. The problem I am having is that the server request that I use SSL, but the server is also configured with a non-standard SSL certificate. The web pages I have found say that I need to install the certificate into the trust store. I don't want to do that (I don't have the necessary permissions.)

  1. Is there a way to get Java to just ignore the certificate error and accept it?
  2. Failing that, is there a way to have the trust store be local for my program, and not installed for the whole JVM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等风也等你 2024-08-05 16:48:07

#1 的工作代码(在 jdk1.6.0_23 中)。

导入

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

实际信任所有TrustManager 代码。

TrustManager trm = new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    public void checkClientTrusted(X509Certificate[] certs, String authType) {

    }

    public void checkServerTrusted(X509Certificate[] certs, String authType) {
    }
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trm }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

Working code ( in jdk1.6.0_23) for #1.

Imports

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

The actual trust all TrustManager code.

TrustManager trm = new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    public void checkClientTrusted(X509Certificate[] certs, String authType) {

    }

    public void checkServerTrusted(X509Certificate[] certs, String authType) {
    }
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trm }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
漫雪独思 2024-08-05 16:48:07

您需要创建一个接受所有证书的假 TrustManager,并将其注册为管理器。 像这样的东西:

public class MyManager implements com.sun.net.ssl.X509TrustManager {
  public boolean isClientTrusted(X509Certificate[] chain) { return true; }
  public boolean isHostTrusted(X509Certificate[] chain) { return true; }
  ...
}


com.sun.net.ssl.TrustManager[] managers =
  new com.sun.net.ssl.TrustManager[] {new MyManager()};

com.sun.net.ssl.SSLContext.getInstance("SSL").
       .init(null, managers, new SecureRandom());

You need to create a fake TrustManager that accepts all certificates, and register it as a manager. Something like this:

public class MyManager implements com.sun.net.ssl.X509TrustManager {
  public boolean isClientTrusted(X509Certificate[] chain) { return true; }
  public boolean isHostTrusted(X509Certificate[] chain) { return true; }
  ...
}


com.sun.net.ssl.TrustManager[] managers =
  new com.sun.net.ssl.TrustManager[] {new MyManager()};

com.sun.net.ssl.SSLContext.getInstance("SSL").
       .init(null, managers, new SecureRandom());
流星番茄 2024-08-05 16:48:07

试试这个(问题 2 的答案):

System.setProperty("javax.net.ssl.trustStore", "/path/to/truststore");

您还可以将其指定为附加命令行参数:

java -Djavax.net.ssl.trustStore=/path/to/truststore <remaining arguments>

在 Fedora 上,这可能是 /etc/pki/java/cacerts 中的系统范围 Java 信任存储

Try this (answer to question 2):

System.setProperty("javax.net.ssl.trustStore", "/path/to/truststore");

You can also specify this as an additional command line parameter:

java -Djavax.net.ssl.trustStore=/path/to/truststore <remaining arguments>

On Fedora this could be the system wide java trust store in /etc/pki/java/cacerts

儭儭莪哋寶赑 2024-08-05 16:48:07

只需将 -Dtrust_all_cert=true 添加到 VM 参数即可。 该参数告诉 java 忽略所有证书检查。

Just add -Dtrust_all_cert=true to VM arguments. This argument tells java to ignore all certificate checks.

甜点 2024-08-05 16:48:07

在命令行中,您可以将参数 -noCertificationCheck 添加到 java 以忽略证书检查。

In Command Line you can add argument -noCertificationCheck to java to ignore the certificate checks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文