CXF RESTful客户端 - 如何信任所有证书?

8

我写过使用“愚蠢”的X509TrustManager和HostnameVerifier的Jersey RESTful客户端,以信任我们实验室系统上的所有自签名SSL证书,以便更轻松地处理证书。

        ClientConfig config = new DefaultClientConfig();
        SSLContext context = null;
        try
        {
            context = SSLContext.getInstance("SSL");
            context.init(null,
                    new TrustManager[] { new DumbX509TrustManager() },
                    null);
            config.getProperties()
                    .put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
                            new HTTPSProperties(this.getHostnameVerifier(),
                                    context));
            webClient = Client.create(config);
        }
        ....

有没有办法使用CXF做类似的事情?
2个回答

10

以下内容来自CXF邮件列表。请注意,由于其他系统更新,我没有必要实现它,因此这是一种理论情况:

WebClient webClient = WebClient.create(this.serviceURL,
    this.username,
    this.password,
    null); // Spring config file - we don't use this

if (trustAllCerts)
{
    HTTPConduit conduit = WebClient.getConfig(webClient)
        .getHttpConduit();

    TLSClientParameters params = 
        conduit.getTlsClientParameters();

    if (params == null) 
    {
        params = new TLSClientParameters();
        conduit.setTlsClientParameters(params);
    }

    params.setTrustManagers(new TrustManager[] { new
        DumbX509TrustManager() }); 

    params.setDisableCNCheck(true);
}

这个答案详细介绍了如何设置一个虚拟的TrustManager,以盲目地接受证书。(当然,你可能不想在生产环境中使用这样的东西) - Eyal

5

接着上一个回答sdoca的内容,这里提供了一个使用简单的X509信任管理器的实现:

import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.transport.http.HTTPConduit;
[...]

public class ApiClient {

    private WebClient webClient;
    [...]

    public void init() {

        webClient = createWebClient(URI).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
        addX509TrustManager();
    }

    private void addX509TrustManager() {
        Assert.notNull(webClient, "Client needs to be initialized");
        HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
        TLSClientParameters params = conduit.getTlsClientParameters();

        if (params == null) {
            params = new TLSClientParameters();
            conduit.setTlsClientParameters(params);
        }

        params.setTrustManagers(new TrustManager[] { new BlindTrustManager() });
        params.setDisableCNCheck(true);
    }

}

BlindTrustManager 被定义如下:

import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;

/**
 * This dumb X509TrustManager trusts all certificate. TThis SHOULD NOT be used in Production. 
 */
public class BlindTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] chain,
            String authType) throws java.security.cert.CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain,
            String authType) throws java.security.cert.CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

这里有一些链接可能对你理解有帮助:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接