Java中创建访问HTTPS的自签名证书的方法
一、问题:
常常在用java访问https的请求时,总是出现SSL禁止的异常。这里给大家教下怎么创建与添加证书。
二、工具 :
1、创建一个目录
2、去Github上搜索InstallCert,然后随便下载一下来。比如:https://github.com/escline/InstallCert 把java文件下载到刚创建的这个目录中。
三、代码:
比如我要添加访问azure的几个管理的域名的https。。
就这样:
1、新建一个cert.bat文件。
2、bat中的内容如下:
javac -d ./ ./InstallCert.java java InstallCert login.chinacloudapi.cn:443 keytool -exportcert -alias login.chinacloudapi.cn-1 -keystore jssecacerts -storepass changeit -file login.chinacloudapi.cn.cer keytool -importcert -alias login.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jre1.8.0_111\lib\security\cacerts" -storepass changeit -file login.chinacloudapi.cn.cer keytool -importcert -alias login.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\security\cacerts" -storepass changeit -file login.chinacloudapi.cn.cer java InstallCert management.core.chinacloudapi.cn:443 keytool -exportcert -alias management.core.chinacloudapi.cn-1 -keystore jssecacerts -storepass changeit -file management.core.chinacloudapi.cn.cer keytool -importcert -alias management.core.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jre1.8.0_111\lib\security\cacerts" -storepass changeit -file management.core.chinacloudapi.cn.cer keytool -importcert -alias management.core.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\security\cacerts" -storepass changeit -file management.core.chinacloudapi.cn.cer java InstallCert management.chinacloudapi.cn:443 keytool -exportcert -alias management.chinacloudapi.cn-1 -keystore jssecacerts -storepass changeit -file management.chinacloudapi.cn.cer keytool -importcert -alias management.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jre1.8.0_111\lib\security\cacerts" -storepass changeit -file management.chinacloudapi.cn.cer keytool -importcert -alias management.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\security\cacerts" -storepass changeit -file management.chinacloudapi.cn.cer java InstallCert graph.chinacloudapi.cn:443 keytool -exportcert -alias graph.chinacloudapi.cn-1 -keystore jssecacerts -storepass changeit -file graph.chinacloudapi.cn.cer keytool -importcert -alias graph.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jre1.8.0_111\lib\security\cacerts" -storepass changeit -file graph.chinacloudapi.cn.cer keytool -importcert -alias graph.chinacloudapi.cn -keystore "C:\Program Files (x86)\Java\jdk1.8.0_66\jre\lib\security\cacerts" -storepass changeit -file graph.chinacloudapi.cn.cer java InstallCert xotrmcninfkeyvault01.vault.azure.cn:443 keytool -exportcert -alias xotrmcninfkeyvault01.vault.azure.cn-1 -keystore jssecacerts -storepass changeit -file xotrmcninfkeyvault01.vault.azure.cn.cer keytool -importcert -alias xotrmcninfkeyvault01.vault.azure.cn -keystore "C:\Program Files (x86)\Java\jre1.8.0_111\lib\security\cacerts" -storepass changeit -file xotrmcninfkeyvault01.vault.azure.cn.cer keytool -importcert -alias xotrmcninfkeyvault01.vault.azure.cn -keystore "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\security\cacerts" -storepass changeit -file xotrmcninfkeyvault01.vault.azure.cn.cer
好了,管理员权限运行一下这个bat按提示输入yes之类的就可以了。
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn