SpringBoot+CXF下Https調用webservice跳過安全證書的配置


最近項目中用到了webservice,需要在代碼中使用https調用,結果報錯了。日志提示:

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

經過不懈的百度,發現是需要安全證書,最后發現一個方法可以跳過證書,成功實現https的調用,現在分享給大家!

import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.ClientLifeCycleListener;
import org.apache.cxf.transport.http.HTTPConduit;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * CXF連接HTTPS跳過安全證書配置
 */
@Configuration
public class SkipSecurityAuthenticationListener implements ClientLifeCycleListener {
    @Override
    public void clientCreated(Client client) {
        if (client.getConduit() instanceof HTTPConduit) {
            HTTPConduit conduit = (HTTPConduit) client.getConduit();

            TLSClientParameters params = conduit.getTlsClientParameters();

            if (params == null) {
                params = new TLSClientParameters();
                conduit.setTlsClientParameters(params);
            }

            params.setTrustManagers(new TrustManager[] {
                    new TrustAllManager() });

            params.setDisableCNCheck(true);
        }
    }

    @Override
    public void clientDestroyed(Client client) {
        // Do Nothing
    }



    private class TrustAllManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }


        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Do Nothing
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM