Solve the Problem of failed https certificate verification and solve the problem of https certificate verification

Source: Internet
Author: User

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.