解决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);

    }
相关推荐
sunfove2 小时前
光网络的立交桥:光开关 (Optical Switch) 原理与主流技术解析
网络
Kevin Wang7274 小时前
欧拉系统服务部署注意事项
网络·windows
min1811234564 小时前
深度伪造内容的检测与溯源技术
大数据·网络·人工智能
汤愈韬5 小时前
NAT策略
网络协议·网络安全·security·huawei
汤愈韬5 小时前
Full Cone Nat
网络·网络协议·网络安全·security·huawei
zbtlink5 小时前
现在还需要带电池的路由器吗?是用来干嘛的?
网络·智能路由器
桌面运维家5 小时前
vDisk配置漂移怎么办?VOI/IDV架构故障快速修复
网络·架构
dalerkd5 小时前
忙里偷闲叙-谈谈最近两年
网络·安全·web安全
汤愈韬6 小时前
NAT ALG (应用层网关)
网络·网络协议·网络安全·security·huawei
运维栈记7 小时前
虚拟化网络的根基-网络命名空间
网络·docker·容器