Java1.7 SSL访问Https occur SSLProtocolException and CertificateException


最近在调用微信公众平台高级群发接口之一:

https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN时,https访问抛出异常:

javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name

异常中出现关键字:unrecognized 未被承认的name。

google之,处理办法,代码加入:

 //bug fiexd for: javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name
 System.setProperty ("jsse.enableSNIExtension", "false");

再次调用该接口,抛出新的异常:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching file.api.weixin.qq.com found.

继续处理,代码加入:

 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());
其中CustomizedHostnameVerifier类如下:

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;


/**
 * HostnameVerifier
 * bug fixed : <http://iteches.com/archives/45015>
 * @author will_awoke
 * @version 2014-8-15
 * @see CustomizedHostnameVerifier
 * @since
 */
public class CustomizedHostnameVerifier implements HostnameVerifier
{

    @Override
    public boolean verify(String arg0, SSLSession arg1)
    {
        return true;
    }

}

原因

and then apply this class to your 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 will still accept it.

简而言之:

因为微信的这个接口是未认证https不安全的,所有需要代码中需要dont verify。



参考:

http://iteches.com/archives/45015

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值