解决SSL握手失败问题:SSLHandshakeException: Received fatal alert: handshake_failure

1.异常情况

异常情况如下:

JDK版本中安全机制导致,不同https安全协议不一致,TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。我方系统是jdk1.7默认使用TLSV1.0,对方系统是jdk1.8默认使用TLSV1.2,导致出现异常。

JDK与TLS版本情况如图所示:

2.解决方法

系统做http请求时,手动将TLS版本号改为1.2即可。

2.1http接口解决方法

java 复制代码
public class HttpsClient {

    private static Log logger = LogFactory.getLog(HttpClientUtil.class);
	
	private volatile static CloseableHttpClient httpsClient = null;

	private HttpsClient(){}
		
	public static CloseableHttpClient getInstance() throws Exception{
		if (httpsClient == null ){
            synchronized (HttpsClient.class){
                if (httpsClient == null ) {               	
                	httpsClient = createSSLHttpClient();
                }
            }
        }
        return httpsClient;
	}

    private static CloseableHttpClient createSSLHttpClient() {
        CloseableHttpClient client = null;

        // 设置ssl兼容协议版本
        SSLConnectionSocketFactory sslsf = null;
        try {
            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, new TrustStrategy() {
                        @Override
                        public boolean isTrusted(X509Certificate[] chain,
                                                 String authType) throws CertificateException {
                            return true;
                        }
                    }).build();
            sslsf = new SSLConnectionSocketFactory(
                    sslContext,
                    new String[] { "TLSv1.2" },
                    null,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            javax.net.ssl.SSLSocketFactory factory = sslContext.getSocketFactory();
            SSLSocket socket = (SSLSocket) factory.createSocket();
            String[] protocols = socket.getSupportedProtocols();
            logger.trace("支持的协议:" + Arrays.asList(protocols));

            client = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();
        } catch (Exception e) {
            logger.error("创建SSLConnectionSocketFactory失败", e);
            e.printStackTrace();
        }

        // 创建httpclient
        if (client == null) {
            logger.error("创建支持SSL的HttpClient失败,创建普通的HttpClient");
            client = HttpClients.createDefault();
        }
        return client;
    }

}

2.2 webservice接口解决方法

java 复制代码
public String submitToOA(KmReviewParamterForm form) throws Exception {
    	ISysCodeService sysCodeService = (ISysCodeService)SpringBeanUtil.getBean("sysCodeService");
    	WebServiceConfig cfg = WebServiceConfig.getInstance();
    	cfg.setAddress(sysCodeService.getContentByCode("reviewUrl").getFdContent());
    	cfg.setUser(sysCodeService.getContentByCode("reviewFinanceUser").getFdContent());
    	cfg.setPassword(sysCodeService.getContentByCode("reviewFinancePassword").getFdContent());
    	IKmReviewWebserviceService service = (IKmReviewWebserviceService) callService(cfg.getAddress(), cfg.getServiceClass());

		//2025-07-18 设置TLSv1.2版本 START
		Client client = ClientProxy.getClient(service);
		HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
		SSLSocketFactory sslSocketFactory = SSLSocketFactoryBuilder.create().setProtocol("TLSv1.2").build();
		TLSClientParameters tlsClientParameters = new TLSClientParameters();
		tlsClientParameters.setSSLSocketFactory(sslSocketFactory);
		httpConduit.setTlsClientParameters(tlsClientParameters);
		//2025-07-18 设置TLSv1.2版本 END

		return service.addReview(form);

    }
相关推荐
0和1的舞者26 分钟前
《网络编程核心概念与 UDP Socket 组件深度解析》
java·开发语言·网络·计算机网络·udp·socket
华普微HOPERF37 分钟前
Matter协议,如何赋能智能家居构建跨生态的互操作网络?
网络·智能家居
YFLICKERH1 小时前
【加密协议】SSL/TLS 协议工作流程
网络协议·ssl/tls
河南博为智能科技有限公司1 小时前
动环监控终端-守护变电站安全运行的智能核心
运维·服务器·网络·物联网
无心水2 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
Top`2 小时前
两个服务之间传递的数据本质上是字节码(Byte Stream)
网络
dyxal3 小时前
非对称加密:彻底解决密钥分发难题的数字安全革命
服务器·网络·安全
少云清5 小时前
【功能测试】4_Web端抓包 _网络知识
网络·功能测试·抓包
观望过往7 小时前
WebSocket 技术全解析:原理、应用与实现
网络·websocket·网络协议
e***75397 小时前
在 Windows 上生成本地 SSL 证书并使用 HTTPS 访问本地 Nginx 服务器
windows·https·ssl