为Apache Camel创建http客户端以调用启用证书的Web服务

时间:2014-02-19 09:41:19

标签: https certificate apache-camel

我有以下https客户端,但它已弃用了方法。当我试图删除它们时,我没有得到回复。 你可以帮助我在没有弃用方法的情况下获得这个客户端。

弃用方法: 的SSLSocketFactory SchemeRegistry 方案 getConnectionManager()

我的客户:

HttpComponent httpComponent = context.getComponent(“https4”,HttpComponent.class);

    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(new File("XXXXXX")), "XXXXXX".toCharArray());
    //keystore.load(new FileInputStream(new File("C:/certificate/ToPankaj/clientcert.jks")), "changeit".toCharArray());

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyFactory.init(keystore, "changeit".toCharArray());


    SSLContext sslcontext = SSLContext.getInstance("SSLv3");
    sslcontext.init(keyFactory.getKeyManagers(), null, null);


    @SuppressWarnings("deprecation")
    SSLSocketFactory factory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    @SuppressWarnings("deprecation")
    SchemeRegistry registry = new SchemeRegistry();

    @SuppressWarnings("deprecation")
    final Scheme scheme = new Scheme("https4", 443, factory);

    //registry.register(scheme);

    httpComponent.setHttpClientConfigurer(new HttpClientConfigurer() 
    {

        @SuppressWarnings("deprcecation")
        @Override
        public void configureHttpClient(HttpClient client) {
            client.getConnectionManager().getSchemeRegistry().register(scheme);
        }
    });

    httpComponent.setClientConnectionManager(new ThreadSafeClientConnManager());
    //httpComponent.
    Endpoint endpoint =httpComponent.createEndpoint(Uri);

    return endpoint;

1 个答案:

答案 0 :(得分:2)

使用Camel doc中的示例:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory;
import org.apache.camel.main.Main;
import org.apache.camel.util.jsse.KeyManagersParameters;
import org.apache.camel.util.jsse.KeyStoreParameters;
import org.apache.camel.util.jsse.SSLContextParameters;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

public class SSLHttpClientRouteBuilder extends RouteBuilder {

    @Override
    public void configure() {
        final KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("/users/home/server/keystore.jks");
        ksp.setPassword("keystorePassword");

        final KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp);
        kmp.setKeyPassword("keyPassword");

        final SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);

        final ProtocolSocketFactory factory = new SSLContextParametersSecureProtocolSocketFactory(scp);

        Protocol.registerProtocol("https", new Protocol("https", factory, 443));

        from("direct:start").to("https://mail.google.com/mail/").to("mock:results");
    }
}

或者,您也可以直接使用Camel doc

中提到的Apache HTTP客户端