3

使用 Websphere 尝试使用 HttpClient 4.x(当前版本为 4.2.1)连接到外部系统时,我们的环境中存在问题。连接到外部系统很好,他们的证书安装在 Websphere 中,无需额外配置 HttpClient。但是,当他们启用相互身份验证时,它不再起作用,我们得到一个 SSLPeerUnverifiedException 异常:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated,
at com.ibm.jsse2.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:105),
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128),
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572),
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180),
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294),
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640),
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479),
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906),
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1066),

我得到了以下代码示例,我想知道是否有任何方法可以将 HttpClient 配置为使用此代码示例那样的显式别名。我试图找到有关使用 HttpClient 4 的 SSL 相互身份验证的良好文档,但找不到太多。

这是代码示例:

private HttpURLConnection getConnection(String server, String machine,
String port) throws Exception {
URL u = new URL(server);
HttpsURLConnection connection = (HttpsURLConnection) u.openConnection();
String alias = "CellDefaultSSLSettings";

final HashMap connectionInfo = new HashMap();
connectionInfo.put(JSSEHelper.CONNECTION_INFO_DIRECTION,
JSSEHelper.DIRECTION_OUTBOUND);
connectionInfo.put(JSSEHelper.CONNECTION_INFO_REMOTE_HOST, machine);
connectionInfo.put(JSSEHelper.CONNECTION_INFO_REMOTE_PORT, port);

javax.net.ssl.SSLSocketFactory sslFact = JSSEHelper.getInstance()
.getSSLSocketFactory(alias, connectionInfo, null);

connection.setSSLSocketFactory(sslFact);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
return connection;
}

基本上,如何让 HttpClient 使用“CellDefaultSSLSettings”?

4

2 回答 2

2

这个问题根本上与HttpClient无关。HttpClient 可以配置为使用任何自定义SSLContextSSLSocketFactory实例建立 HTTPS 连接。这基本上是关于如何使用 JSSE API 以SSLContext正确的方式进行配置。在您的特定情况下JSSEHelper,为您完成所有艰苦的工作。

// JSSE socket factory
javax.net.ssl.SSLSocketFactory jssesf = JSSEHelper.getInstance().getSSLSocketFactory(alias, connectionInfo, null);
// HC socket factory
SSLSocketFactory hcsf = new SSLSocketFactory(jssesf, SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

这将提供一个可以向连接管理器注册的连接套接字工厂。

HttpClient 4.3 还带有 SSLContextBuilder 类,可用于使用流体构建器 API 组装自定义 SSL 配置。

https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLContextBuilder.java

于 2013-06-20T14:55:38.130 回答
0

奥列格的回答帮助了我。

我所做的是扩展 DefaultHttpClient,每个构造函数都为目标 URL 获取一个 String 参数并调用一个方法 setupScheme:

private void setupScheme(final String url) throws Exception {
    Scheme scheme = new Scheme("https", 443, retrieveWebsphereSSLConnectionFactory(url));
    getConnectionManager().getSchemeRegistry().register(scheme);
}

方法retrieveWebsphereSSLConnectionFactory 实质上将示例中的代码与提供的代码oleg 结合在一起:

private SchemeSocketFactory retrieveWebsphereSSLConnectionFactory(final String url)
        throws SSLException, URISyntaxException {
    final String alias = "CellDefaultSSLSettings";

    final HashMap<String, String> connectionInfo = new HashMap<String, String>();
    connectionInfo.put(JSSEHelper.CONNECTION_INFO_DIRECTION, JSSEHelper.DIRECTION_OUTBOUND);
    connectionInfo.put(JSSEHelper.CONNECTION_INFO_REMOTE_HOST,
            URIUtils.extractHost(new URI(url)).getHostName());
    connectionInfo.put(JSSEHelper.CONNECTION_INFO_REMOTE_PORT, "443");

    return new SSLSocketFactory(JSSEHelper.getInstance().getSSLSocketFactory(alias, connectionInfo, null),
            SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
}
于 2013-06-24T17:17:19.303 回答