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方法失败:");
}
相关推荐
EasyDSS1 小时前
国标GB28181-2022平台EasyGBS:安防监控中P2P的穿透方法
网络协议·php·音视频·p2p
网安墨雨2 小时前
常用网络协议
网络·网络协议
ZoeLandia4 小时前
WebSocket | 背景 概念 原理 使用 优缺点及适用场景
网络·websocket·网络协议
Crossoads8 小时前
【汇编语言】端口 —— 「从端口到时间:一文了解CMOS RAM与汇编指令的交汇」
android·java·汇编·深度学习·网络协议·机器学习·汇编语言
诸葛悠闲8 小时前
SOME/IP 协议详解——信息格式
网络协议
gywl10 小时前
openEuler VM虚拟机操作(期末考试)
linux·服务器·网络·windows·http·centos
某柚啊11 小时前
Windows开启IIS后依然出现http error 503.the service is unavailable
windows·http
_oP_i11 小时前
HTTP 请求Media typetext/plain application/json text/json区别
网络协议·http·json
yang_shengy12 小时前
【JavaEE】网络(6)
服务器·网络·http·https
OkeyProxy12 小时前
HTTP、HTTPS和SOCKS5代理協議
网络协议·https·云计算·代理服务器·海外ip代理