JAVA忽略证书返回403,java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory

packagecom.tgb.mq.producer.utils;importjava.io.IOException;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.net.Socket;importjava.net.SocketAddress;importjava.net.UnknownHostException;importjava.security.KeyManagementException;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;importjavax.net.ssl.SSLContext;importjavax.net.ssl.SSLSocketFactory;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importorg.apache.commons.httpclient.ConnectTimeoutException;importorg.apache.commons.httpclient.params.HttpConnectionParams;importorg.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;public class MySSLProtocolSocketFactory implementsSecureProtocolSocketFactory {private static final Logger logger = LoggerFactory.getLogger(MySSLProtocolSocketFactory.class);private static SSLContext context = null;

SSLContext createSSLContext() {try{

context= SSLContext.getInstance("SSL");

context.init(null, new TrustManager[] { new TrustAnyTrustManager() }, newSecureRandom());

}catch(NoSuchAlgorithmException e) {

logger.debug(e.getMessage());

}catch(KeyManagementException e) {

logger.debug(e.getMessage());

}returncontext;

}

SSLContext getSSLContext() {if (context != null) {returncreateSSLContext();

}else{returncontext;

}

}

@Overridepublic Socket createSocket(String host, int port) throwsIOException, UnknownHostException {returngetSSLContext().getSocketFactory().createSocket(host, port);

}

@Overridepublic Socket createSocket(String host, int port, InetAddress localHost, int localPort) throwsIOException, UnknownHostException {returngetSSLContext().getSocketFactory().createSocket(host, port, localHost, localPort);

}

@Overridepublic Socket createSocket(Socket socket, String host, int port, boolean autoClose) throwsIOException, UnknownHostException {returngetSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);

}

@Overridepublic Socket createSocket(String host, int port, InetAddress localHost, intlocalPort, HttpConnectionParams arg4)throwsIOException, UnknownHostException, ConnectTimeoutException {if (arg4 == null) {try{throw new Exception("参数为空");

}catch(Exception e) {

logger.debug(e.getMessage());

}

}int timeout =arg4.getConnectionTimeout();

SSLSocketFactory socketFactory=getSSLContext().getSocketFactory();if (timeout == 0) {returnsocketFactory.createSocket(host, port, localHost, localPort);

}else{

Socket socket=socketFactory.createSocket();

SocketAddress localAdd= newInetSocketAddress(localHost, localPort);

SocketAddress remoteAdd= newInetSocketAddress(host, port);

socket.bind(remoteAdd);

socket.connect(localAdd, timeout);returnsocket;

}

}//自定义

private static class TrustAnyTrustManager implementsX509TrustManager {

@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throwsCertificateException {//重写x509TrustManager中的checkClentTrusted方法,为空默认客户端是可信的

}

@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throwsCertificateException {//重写x509TrustManager中的checkServerTrusted方法,为空默认服务器是可信的

}

@OverridepublicX509Certificate[] getAcceptedIssuers() {//重写x509TrustManager中的getAcceptedIssuers方法,默认接受发行人的证书为空

return newX509Certificate[] {};

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值