1、报错信息
java.security.cert.CertificateException: No name matching api.weibo.com found; nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching api.weibo.com found
原因:在挪用api.weibo.com的时候,我们利用的是https的方法,正常环境下应该是利用api.weibo.com的证书,但由于某些原因,我们只能利用本身的证书,劳务派遣管理系统,导致在验证证书的时候,软件开发,就报了这个错误。
办理的步伐:忽略处事端和客户端的证书校验即可。java 提供的相关的类。
2、详细实现方法
通过重写TrustManager的checkClientTrusted(查抄客户端证书信任)和checkServerTrusted(检点处事端证书验证)。
以及HostnameVerifier的verify(校验)要领即可打消对质书的所有验证。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.*; import java.io.IOException; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public final class DisableSSLCertificateCheckUtil { private static final Logger LOGGER = LoggerFactory.getLogger(DisableSSLCertificateCheckUtil.class); /** * Prevent instantiation of utility class. */ private DisableSSLCertificateCheckUtil() { } /** * Disable trust checks for SSL connections. */ public static void disableChecks() { try { new URL("https://0.0.0.0/").getContent(); } catch (IOException e) { // This invocation will always fail, but it will register the // default SSL provider to the URL class. } try { SSLContext sslc; sslc = SSLContext.getInstance("TLS"); TrustManager[] trustManagerArray = {new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}; sslc.init(null, trustManagerArray, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); } catch (Exception e) { LOGGER.error("error msg:{}", e); throw new IllegalArgumentException("证书校验异常!"); } } }
挪用方法:
DisableSSLCertificateCheckUtil.disableChecks();
影响的范畴:将会影响整个tomcat内里对质书的验证。即通过tomcat内里的其他项目固然没有执行这一段代码可是也同样会忽略证书的验证。
影响的时间:执行这段代码之后的所有时间都生效。
叩谢:感激您的耐性阅读!
,软件开发