我想在使用CXF客户端(3.1.2)进行休息呼叫时设置自定义SNI主机名(SNI配置)。我正在使用java 8.我能够使用HTTPClient做同样的事情(参见下面的强文本代码剪切以供参考),但我无法弄清楚如何使用CXF客户端做同样的事情。
//对于HTTP客户端
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;
}
使用org.apache.cxf.configuration.jsse.TLSClientParameters类和public final void setCertAlias(String ctAlias)方法设置服务器端使用的证书别名,这在密钥库有多个证书时很有用。希望这对您有所帮助。