SOAP 客户端 SSL,身份验证失败

SOAP Client SSL, failed authentication

我正在组装一个 soap 客户端来调用第三方 soap 服务。我在连接 Java 时遇到问题。它适用于 SoapUI。这是我第一次在应用程序中设置密钥库。我找到的所有代码都是一样的,而且非常简单,但我无法弄清楚为什么 java 版本不起作用。我正在使用由我提供服务的公司提供的 TLS pfx 文件也在尝试连接。 我从服务器返回 403。这是代码

        URL wsdlLocation = new URL(SECURE_INTEGRATION_WSDL);
        ObjectFactory ofactory = new ObjectFactory();
        HttpsURLConnection httpsConnection = (HttpsURLConnection)wsdlLocation.openConnection();
        char[] password = CLIENT_KEYSTORE_PASSWORD.toCharArray();

        //load keystore
        FileInputStream is = new FileInputStream(new File(CLIENT_KEYSTORE_PATH));
        final KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(is, password);
        is.close();

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");

        kmf.init(keystore, password);

        //set the ssl context
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), null,
                new java.security.SecureRandom());


        httpsConnection.setSSLSocketFactory(sc.getSocketFactory());



        SecureIntegrationServicesImap client = new SecureIntegrationServicesImap(wsdlLocation);


        SesMessage message = ofactory.createSesMessage();

        ReceiveRequest r = ofactory.createReceiveRequest();

        r.setEmail(ofactory.createReceiveRequestEmail("<email ommitted>"));
    ArrayOfMessageSummary messages = client.getWSHttpBindingSecureIntegrationServiceImap().getMessageList(r);
    log.info(messages.getMessageSummary().size());

非常感谢对我的错误的任何帮助..

不确定这是否重要,但服务器是 .NET 平台

这是我得到的堆栈跟踪

javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://<host omitted>/TS?wsdl. It failed with: 
Server returned HTTP response code: 403 for URL: https://<host omitted>/TS?wsdl.
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:265)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:246)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:209)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:178)
at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:363)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:321)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:230)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:211)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:207)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:114)
at javax.xml.ws.Service.<init>(Service.java:77)
at org.tempuri.SecureIntegrationServicesImap.<init>(SecureIntegrationServicesImap.java:50)
at com.wiredinformatics.utils.SecureExchange.main(SecureExchange.java:127) Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://host omitted/TS?wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:999)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:400)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:231)
... 11 more

听起来您正在使用基于 TLS 的客户端身份验证。根据您发布的代码,我怀疑问题是您在初始化后没有在任何地方使用 httpsConnection 。因此,它不会像您期望的那样尝试使用您的客户端证书,而是使用默认的请求上下文设置。

假设您使用的是 JAX-WS,您应该能够使用 this answer 中概述的解决方案将您的证书绑定到您的请求上下文(而不是初始化您自己的 HttpsURLConnection):

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());