如何使用httpClient4使用Axis2配置SSL和HTTP代理

时间:2018-11-23 07:42:39

标签: ssl axis2 apache-httpclient-4.x

我想使用axis2 1.7.8通过https(443端口)通过http代理(端口8080)连接到Web服务。 我读了文章https://stackoverflow.com/a/53171162/1692626,其中描述了如何配置httpclient 4和SSL。 我试图通过HttpTransportProperties.ProxyProperties将代理设置为选项,但是没有运气。我还尝试将代理设置为直接作为httphost的Httphost到httpclient(httpclient.getParams()。setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy)并注册其他http端口8080,但是我遇到了诸如“无法识别的SSL消息,纯文本连接?”之类的异常。

正确的做法是什么?

这是我最后一次插入的代码

    public static  <Axis2Stub extends Stub> void initHttpsStub(Axis2Stub stub, String proxyIp, Integer proxyPort, long connectionTimeout, long connectionRequestTimeout, long socketTimeout, String trustStore, String trustStorePass){
    if(stub != null){
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CACHED_HTTP_CLIENT,
                getHttpClient(proxyIp, proxyPort, trustStore, trustStorePass, connectionTimeout, connectionRequestTimeout, socketTimeout));
    }
}

private static HttpClient getHttpClient(String proxyIp, Integer proxyPort, String trustStorePath, String trustStorePass, long connectionTimeout, long connectionRequestTimeout, long socketTimeout){

    try {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        LOG.debug("Loading keystore file:{}", trustStorePath);
        keyStore.load(new FileInputStream(new File(trustStorePath)), trustStorePass.toCharArray());

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, trustStorePass.toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        if(proxyPort != null) {
            Scheme httpScheme = new Scheme("http", proxyPort, sf);
            schemeRegistry.register(httpScheme);
        }
        Scheme httpsScheme = new Scheme("https", 443, sf);
        schemeRegistry.register(httpsScheme);
        ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        HttpClient httpClient = new DefaultHttpClient(cm);

        HttpParams params = httpClient.getParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
        params.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectionRequestTimeout);

        if(proxyIp != null && proxyPort != null){
            HttpHost proxy = new HttpHost(proxyIp, proxyPort);
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }

        return httpClient;
    }catch (GeneralSecurityException | IOException ex){
        throw new MwRuntimeException(ex, "1", "Unable to initialize webservice client: " + ex.getMessage());
    }
}

还有一个堆栈跟踪

2018-11-22 09:18:46,023 INFO  [org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl] (default task-1) [b4a5d3ca-318d-4869-84ff-a2f7aa71ea81]    Unable to sendViaPost to url[https://XXXXXXXXXXXXXXXXXXX]: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
        at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
        at sun.security.ssl.InputRecord.read(InputRecord.java:527)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
        at org.apache.http.conn.ssl.SSLSocketFactory.createLayeredSocket(SSLSocketFactory.java:573)
        at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:557)
        at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:414)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
        at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
        at org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.executeMethod(HTTPSenderImpl.java:873)
        at org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.sendViaPost(HTTPSenderImpl.java:238)
        at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:121)
        at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:403)
        at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:234)
        at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:431)
        at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:399)
        at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
        at org.apache.axis2.client.OperationClient.execute(OperationClient.java:150)
        at xxxxx.Stub.getID(Stub.java:173)

我试图删除http端口寄存器,但是随后我得到了缺少http配置的异常。 该应用程序可在服务器wildfly12上运行。我知道我可以使用cxf,但是我不能更改它

0 个答案:

没有答案