English 中文(简体)
CXF 知识客户——如何信任所有方面?
原标题:CXF RESTful Client - How to do trust all certs?

我已写到泽西里特客,他们使用Dumb。 X509TrustManager 和Hotherifier 信任我们实验室系统的所有SSLcer,以便更容易处理自签的cer。

        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做类似的事情?

最佳回答

这是来自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);
}
问题回答

为了完成理论的回答,这里是一位 dX509信托经理执行:

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);
    }

}

Where BlindTrustManager is defined as follows:

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;
    }
}

为了更好地了解:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签