CXF调用webservice跳过https认证

1、相关依赖

powershell 复制代码
compile ('org.apache.cxf:cxf-spring-boot-starter-jaxws:3.5.5')

2、添加配置

java 复制代码
package com.tele.health.operation.api.listener;


import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transport.http.HTTPConduitConfigurer;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.context.annotation.Configuration;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

@Configuration
public class SoapHTTPConduitConfigurer implements HTTPConduitConfigurer {

        public void configure(String name, String address, HTTPConduit c) {

            try{

                HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//                httpClientPolicy.setConnectionTimeout(itfConfig.getConnectionTimeout());
//                httpClientPolicy.setReceiveTimeout(itfConfig.getSoTimeout());
                c.setClient(httpClientPolicy);
                SSLContext context = createIgnoreVerifySSL();
                TLSClientParameters params = c.getTlsClientParameters();
                if (params == null) {
                    params = new TLSClientParameters();
                }

                params.setSslContext(context);
                //SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(context,
                 //       new String[] { "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
                params.setSSLSocketFactory(context.getSocketFactory());
                params.setHostnameVerifier((s, sslSession) -> true);
                c.setTlsClientParameters(params);
            }catch(Exception e){
                e.printStackTrace();
            }


        }

    public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sc = SSLContext.getInstance("TLS");

        // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sc.init(null, new TrustManager[]{trustManager}, null);
        return sc;
    }
}

3、调用webservice

java 复制代码
BusFactory bf = CXFBusFactory.newInstance();
Bus bus = bf.createBus();
bus.setExtension(new SoapHTTPConduitConfigurer(), HTTPConduitConfigurer.class);
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(bus);
Client client =null;
try{
   client = dcf.createClient("https://ip:端口/csp/hsb/DHC.Published.PUB0002.BS.PUB0002.CLS?WSDL=1");
}catch(Exception e){
    e.printStackTrace();
    logger.info("webservice连接失败,失败原因:"+e.getMessage());
}
Object[] objects = new Object[0];
try {
  objects = client.invoke("方法名","参数");
}catch(Exception e){
   e.printStackTrace();
   logger.error("调用webservic方法失败:");
}
相关推荐
草莓熊Lotso1 小时前
unordered_map/unordered_set 使用指南:差异、性能与场景选择
java·开发语言·c++·人工智能·经验分享·python·网络协议
二狗mao9 小时前
Uniapp使用websocket进行ai回答的流式输出
websocket·网络协议·uni-app
1560820721914 小时前
基于7VX690T FPGA实现万兆TCP/IP资源和性能测试
网络协议·tcp/ip·fpga开发
赖small强15 小时前
【Linux 网络基础】libwebsockets HTTPS 服务端实现机制详解
linux·网络·https·tls·libwebsockets
布朗克16817 小时前
HTTP 与 HTTPS 的工作原理及其区别
http·https
踏浪无痕17 小时前
线上偶发 502 排查:用 Netty 成功复现 KeepAlive 时间窗口案例实战(附完整源码)
运维·网络协议
北京耐用通信17 小时前
告别“牵一发而动全身”:耐达讯自动化Profibus PA分线器为石化流量计网络构筑安全屏障
人工智能·网络协议·安全·自动化·信息与通信
Sinowintop17 小时前
易连EDI-EasyLink无缝集成之消息队列Kafka
分布式·网络协议·kafka·集成·国产化·as2·国产edi
赖small强21 小时前
【Linux 网络基础】HTTPS 技术文档
linux·网络·https·tls
阿巴~阿巴~1 天前
自定义协议设计与实践:从协议必要性到JSON流式处理
服务器·网络·网络协议·json·操作系统·自定义协议