Caused by: java.security.cert.CertificateException: No subject alternative
思路安装证书
1.查询jdk安装路径
which java
[root@localhost ~]# which java
/usr/bin/java
执行ls -lrt /usr/bin/java
[root@localhost ~]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 10月 10 08:06 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 10月 10 08:06 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64/jre/bin/java
在可执行 java命令的情况下查找过程如下:
2.下载证书
从网站直接导出https CA证书 base64
3.安装证书
上传至服务器/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64/jre/lib/security
keytool -import -keystore cacerts -storepass changeit -keypass changeit -alias xxx -file xxx.cer;
-alias 指定别名(推荐和证书同名)
-keystore 指定存储文件(此处固定)
-file 指定证书文件全路径(证书文件所在的目录)
确认信任 yes
查看证书是否已安装信任
keytool -list -keystore cacerts -alias XXX
keytool -delete -alias XXX -keystore cacerts
keytool -import -alias XXX -keystore cacerts -file ${JAVA_HOME}/jre/lib/security/XXX.cer
keytool -list -keystore cacerts -alias XXX
4.运行测试
5.针对没有域名的https选用跳过认证
@Component
public class HttpsUtil {
private static String url;
@Value("${aqy.config.url}")
public void setUrl(String url) {
HttpsUtil.url = url;
}
private static Logger logger = LoggerFactory.getLogger(HttpsUtil.class);
static CloseableHttpClient httpClient;
static CloseableHttpResponse httpResponse;
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
/**
* 发送https请求
*
* @throws Exception
*/
public static String postByHttp(String url, Map<String, String> paramMap,
Map<String, String> headers) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
if (paramMap != null) {
// 添加请求参数
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
}
HttpPost httpPost = new HttpPost(uriBuilder.build());
for (Map.Entry item : headers.entrySet()) {
httpPost.setHeader(item.getKey().toString(), item.getValue().toString());//设置header
}
httpClient = HttpsUtil.createSSLClientDefault();
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
return jsObject;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
httpResponse.close();
httpClient.close();
logger.info("请求流关闭完成");
} catch (IOException e) {
logger.info("请求流关闭出错");
e.printStackTrace();
}
}
}
public static String getByHttp(String requestUrl, String requestMethod, String outputStr, HashMap<String, Object> map) {
try {
requestUrl = url + requestUrl;
String randomCode = CipherTextUtil.randomCode();
System.out.println("requestUrl = " + requestUrl);
map.put("cipherText", CipherTextUtil.getMD5(randomCode));
//10位时间戳
map.put("timestamp", CipherTextUtil.getZeroPointDate());
//随机序列(长度为 10 位的数字序列)
map.put("randomSeries", randomCode);
HttpGet httpGet = new HttpGet(requestUrl);
for (Map.Entry item : map.entrySet()) {
httpGet.setHeader(item.getKey().toString(), item.getValue().toString());//设置header
}
httpClient = HttpsUtil.createSSLClientDefault();
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
return jsObject;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
httpResponse.close();
httpClient.close();
logger.info("请求流关闭完成");
} catch (IOException e) {
logger.info("请求流关闭出错");
e.printStackTrace();
}
}
}
public static String getByHttp(String requestUrl, HashMap<String, Object> map) {
try {
HttpGet httpGet = new HttpGet(requestUrl);
for (Map.Entry item : map.entrySet()) {
httpGet.setHeader(item.getKey().toString(), item.getValue().toString());//设置header
}
httpClient = HttpsUtil.createSSLClientDefault();
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
return jsObject;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
httpResponse.close();
httpClient.close();
logger.info("请求流关闭完成");
} catch (IOException e) {
logger.info("请求流关闭出错");
e.printStackTrace();
}
}
}
}