JAX WS客户端和SSL握手异常

时间:2014-01-20 13:59:40

标签: java web-services ssl jax-ws

我使用一个wsdl文件创建了一个JAX-WS客户端。一切在localhost上工作正常,但在服务器上没有。 我收到引起:javax.net.ssl.SSLHandshakeException ... 引起:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在sun.security.validator.PKIXValidator上找到所请求目标的有效证书路径< /强>

我知道,我的JAX-WS客户端不信任证书。所以我在这里找到了一个快速而肮脏的解决方案:http://ws.apache.org/xmlrpc/ssl.html并试图实现它:

ApiVersion1Service apiVersion1Service = new ApiVersion1Service(wsdlURL, SERVICE_NAME);
    APIport = apiVersion1Service.getApiVersion1Port();

    // SOAP Header
    BindingProvider bindingProvider = (BindingProvider) APIport;
    Binding binding = bindingProvider.getBinding();
    Map<String, Object> requestContext = bindingProvider.getRequestContext();

    requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL + accountID);
    List<Handler> handlers = binding.getHandlerChain();
    handlers.add(new SOAPAuthenticationHandler(username, password));
    binding.setHandlerChain(handlers);

    // SSL
    SSLContext context = SSLContext.getInstance("SSL");
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }
        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }
    } };
    context.init(null, trustAllCerts, new SecureRandom());
    SSLSocketFactory sslSocketFactory = context.getSocketFactory(); 
    bindingProvider.getRequestContext().put(/*JAXWSProperties.SSL_SOCKET_FACTORY*/ "com.sun.xml.ws.transport.https.client.SSLSocketFactory" , sslSocketFactory);

....但我仍然得到SSLHandshakeException。你有什么主意吗? 谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:-1)

public class VerifyEverythingHostnameVerifier
        implements HostnameVerifier {

    @Override
    public boolean verify(String string, SSLSession sslSession) {
        return true;
    }
}

使用:

 HttpsURLConnection.setDefaultHostnameVerifier(new VerifyEverythingHostnameVerifier());