Solve the Problem of failed https certificate verification and solve the problem of https certificate verification
1. error message
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
Cause: when calling api.weibo.com, We Use https. Normally, we should use the api.weibo.com certificate. But for some reason, we can only use our own certificate, this error is reported when the certificate is verified.
Solution: Ignore the certificate verification on the server and client. Java-related classes.
2. Specific implementation methods
Rewrite the checkClientTrusted (check client certificate trust) and checkServerTrusted (check server certificate verification) of TrustManager ).
And the verify (verification) method of HostnameVerifier can cancel all verification on the certificate.
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 util Ity 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 (parts [] chain, String authType) throws CertificateException {} @ Override public void checkServerTrusted (parts [] chain, string authType) throws CertificateException {}@ Override public X509Certificate [] getAcceptedIssuers () {return new X509Certificate [0] ;}}; sslc. init (null, trustManag ErArray, 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 ("certificate verification exception! ");}}}
Call method:
DisableSSLCertificateCheckUtil.disableChecks();
Impact Scope: It will affect the authentication of certificates in tomcat. That is, although this code is not executed by other projects in tomcat, the certificate verification is also ignored.
Time of impact: This Code takes effect all the time after it is executed.
Thank you! Thank you for your patience!