java 自签名_在Java客户机中接受服务器的自签名SSL证书

这里基本上有两个选项:将自签名证书添加到jvm信任库,或者将客户端配置为

备选案文1

从浏览器导出证书并将其导入JVM信任库(以建立信任链):\bin\keytool -import -v -trustcacerts-alias server-alias -file server.cer-keystore cacerts.jks -keypass changeit-storepass changeit

备选案文2

禁用证书验证:// Create a trust manager that does not validate certificate chainsTrustManager[] trustAllCerts = new TrustManager[] {

new X509TrustManager() {

public java.security.cert.X509Certificate[] getAcceptedIssuers() {

return new X509Certificate[0];

}

public void checkClientTrusted(

java.security.cert.X509Certificate[] certs, String authType) {

}

public void checkServerTrusted(

java.security.cert.X509Certificate[] certs, String authType) {

}

} }; // Install the all-trusting trust managertry {

SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());} catch (GeneralSecurityException e) {}

// Now you can access an https URL without having the certificate in the truststoretry {

URL url = new URL("https://hostname/index.html"); } catch (MalformedURLException e) {}

请注意我根本不推荐选项2。..禁用信任管理器会破坏SSL的某些部分,使您在中间攻击中容易受到人为攻击。更喜欢选项1,或者更好的是,让服务器使用由知名CA签名的“真实”证书。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值