Java1.7 SSL access HTTPS occur sslprotocolexception and certificateexception

Source: Internet
Author: User
Tags ssl connection


Recently in the public platform call one of the advanced Mass Interface:

When Https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN, HTTPS access throws an exception:

Javax.net.ssl.SSLProtocolException:handshake Alert:unrecognized_name

The keyword appears in the exception: unrecognized is not recognized by name.

Google, how to handle, code to add:

Bug Fiexd for:javax.net.ssl.SSLProtocolException:handshake alert:unrecognized_name system.setproperty (" Jsse.enablesniextension "," false ");

Call the interface again to throw a new exception:

Javax.net.ssl.SSLHandshakeException:java.security.cert.CertificateException:No Subject Alternative DNS name Matching file.api.weixin.qq.com found.

Continue processing, add code:

URL url = new URL (requesturl); Httpurlconn = (httpsurlconnection) url.openconnection (); Httpurlconn.setsslsocketfactory (SSF); Bug fixed for:java.security.cert.CertificateException:No Subject Alternative DNS name matching Httpurlconn.sethostnameverifier (New Customizedhostnameverifier ());
where the Customizedhostnameverifier class is as follows:

Import Javax.net.ssl.hostnameverifier;import javax.net.ssl.sslsession;/** * hostnameverifier * bug fixed: 

cause:

And then apply the this class to your a single SSL connection
Httpsurlconnection connection = (httpsurlconnection) New URL ("
Https://url "). OpenConnection ();
Connection.sethostnameverifier (New Customizedhostnameverifier ());
or apply to all SLL connection
Httpsurlconnection.setdefaulthostnameverifier (New Customizedhostnameverifier ());
However this method might pose a security risk because basically we don ' t verify the hostname anymore. The server may use other website's certificate and the program would still accept it.

Nutshell:

Because this interface is not authenticated HTTPS is not secure, all need to dont verify in the code.



Reference:

http://iteches.com/archives/45015

http://stackoverflow.com/questions/7615645/ssl-handshake-alert-unrecognized-name-error-since-upgrade-to-java-1-7-0



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.