CXF调用webservice跳过https认证

1、相关依赖

compile ('org.apache.cxf:cxf-spring-boot-starter-jaxws:3.5.5')

2、添加配置

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

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方法失败:");
}

你可能感兴趣的:(https,网络协议,http)