我正在尝试编写一个使用javax.mail API发送邮件的SSL客户端。我遇到的问题是服务器请求我使用SSL,但服务器也配置了非标准的SSL证书。我发现的网页说我需要将证书安装到信任库中。我不想这样做(我没有必要的权限。)
答案 0 :(得分:29)
#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());
答案 1 :(得分:19)
您需要创建一个接受所有证书的假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());
答案 2 :(得分:7)
试试这个(回答问题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
答案 3 :(得分:-2)
只需将IsWOW64 = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
IsNotWOW64 = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
添加到VM参数即可。该参数告诉java忽略所有证书检查。
答案 4 :(得分:-3)
在命令行中,您可以向java添加参数-noCertificationCheck
以忽略证书检查。