最近项目中用到了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; } } }