SNI configuration in cxf client(3.1.2)

2019-04-14 02:07发布

问题:

I want to set custom SNI hostname(SNI configuration) while making rest call using CXF client(3.1.2). I'm using java 8.I'm able to do the same thing using HTTPClient(see below strong textcode snipped for reference), but I'm not able to figure out how to do the same using CXF client.

// For HTTP client

 private SSLConnectionSocketFactory createSSLConnectionSocketFactory(String sniHostanme,
        SSLContext sslContext){

    // Fix for host name verifier, need to implement----------------------
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier()) {
        @Override
        protected void prepareSocket(SSLSocket socket) throws IOException {
            try {
        //      System.out.println("************ setting socket HOST property *************");

                // If SNI is required
                if (StringUtils.isNotBlank(sniHostanme)) {
                    log.debug("SNI HOSTNAME = "+sniHostanme);

                    List<SNIServerName> sniServerNames = new ArrayList<>();
                    sniServerNames.add(new SNIHostName(sniHostanme));

                    SSLParameters sslParam = new SSLParameters();
                    sslParam.setServerNames(sniServerNames);
                    socket.setSSLParameters(sslParam);
                }
                // PropertyUtils.setProperty(socket, "host", "ws.mastercard.com");
            } catch (Exception ex) {
                log.error(ex.getMessage());
            }
            // super.prepareSocket(socket);
        }

    };
    return sslsf;
}

回答1:

Use org.apache.cxf.configuration.jsse.TLSClientParameters class and public final void setCertAlias(String ctAlias) method to set the cert alias used on server side this is useful when keystore has multiple certs.Hopefully this will help you.