我需要调用和休息由第三方托管的端点。他们的服务仅支持 TLSv1,目前不支持任何更新版本。我尝试设置 webclient 配置以将其用于 TLSV1,如下所示。
WebClient client = WebClient
.create(serviceEndPoint,
Collections.singletonList(new JacksonJsonProvider()));
client.path(requestTokenService);
HTTPConduit conduit =
(HTTPConduit) WebClient.getConfig(client).getConduit();
HTTPClientPolicy policy = conduit.getClient();
try {
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, null, null);
TLSClientParameters tlsParams = new TLSClientParameters();
tlsParams.setSSLSocketFactory(sslContext.getSocketFactory());
tlsParams.setSecureSocketProtocol("TLSv1");
conduit.setTlsClientParameters(tlsParams);
logger.info("TLS set to 1.0");
} catch (NoSuchAlgorithmException e) {
logger.error("Setting the tls failed with NoSuchAlgorithmException ["+e.getMessage()+"]");
} catch (KeyManagementException e) {
logger.error("Setting the tls failed with KeyManagementException ["+e.getMessage()+"]");
}
policy.setProxyServer(proxyUrl);
policy.setProxyServerPort(proxyPort);
ClientConfiguration config = WebClient.getConfig(client);
config.getInInterceptors().add(new LoggingInInterceptor());
config.getOutInterceptors().add(new LoggingOutInterceptor());
Response response = client.get();
调用 get 方法时出现以下错误。
2018-01-03 05:30:19,207 [eb46095f-71d9-4a3f-98c3-beec8d97dbf5] ERROR [ServiceClass] - Persist failed with error [javax.net.ssl.SSLHandshakeException: SSLHandshakeException invoking https:abc.com/thepath: Server chose TLSv1, but that protocol version is not enabled or not supported by the client.]
如能提供有关如何在此连接上启用 TLSv1 的帮助,我们将不胜感激。
最佳答案
参见 Configure Oracle's JDK and JRE Cryptography Algorithms ,标题为如何在客户端更改协议(protocol)版本 的部分。
相关文字是:
有几个选项可用于更改 JDK 中的默认客户端 TLS 协议(protocol)版本。
选项 1. 使用“jdk.tls.client.protocols”系统属性
此属性在 7u95 中引入到 JDK 7,在 6u121 中引入到 JDK 6。
要在客户端启用特定的 TLS 协议(protocol),请在引号内以逗号分隔的列表中指定它们;然后在客户端上禁用所有其他支持的协议(protocol)。例如,如果此属性的值为“TLSv1.1,TLSv1.2”,则在客户端启用 TLSv1.1 和 TLSv1.2 的默认协议(protocol)设置,同时启用 SSLv3、TLSv1 和 SSLv2Hello在客户端禁用。
// Set the client default protocol versions to TLS 1.0, 1.1 and 1.2.
$ java -Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" myApp
// Set the client default protocol version to TLS 1.0.
$ java -Djdk.tls.client.protocols="TLSv1" myApp
请注意,JDK 中使用的标准 TLS 协议(protocol)版本名称为 SSLv3、TLSv1、TLSv1.1 和 TLSv1.2。
方案2.使用SSLContext设置TLS版本
“TLSv1.2”协议(protocol)的SSLContext支持TLS 1.2。例如:
// Get SSLContext instance for "TLSv1.2".
SSLContext context = SSLContext.getInstance("TLSv1.2");
// Create SSLEngine object that enables TLS version 1.2.
SSLEngine sslEngine = context.createSSLEngine("www.example.com", 443);
或者
// Create SSLSocket object that enables TLS version 1.2.
SSLSocketFactory socketFac = context.getSocketFactory();
SSLSocekt sslSocket = (SSLSocekt)socketFac.createSocket("www.example.com", 443);
An SSLContext with "TLSv1" protocol supports TLS versions up to TLS 1.0 (no TLS 1.1 and 1.2).
An SSLContext created with "TLSv1.1" supports versions up to TLS 1.1 (no TLS 1.2).
// Get SSLContext instance that supports TLS versions up to TLS 1.0.
SSLContext context = SSLContext.getInstance("TLSv1");
选项 3. 使用 SSLSocket/SSLEngine.setEnabledProtocols() API
应用程序可以在 SSLSocket/SSLEngine 对象中显式设置启用的协议(protocol)。例如:
// Enable TLS 1.0, 1.1 and 1.2 in an SSLSocket object.
sslSocket.setEnabledProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
// Enable TLS 1.0, 1.1 and 1.2 in an SSLEngine object.
sslEngine.setEnabledProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
或者
// Enable TLS 1.0 only in an SSLSocket object.
sslSocket.setEnabledProtocols(new String[] {"TLSv1"});
// Enable TLS 1.0 only in an SSLEngine object.
sslEngine.setEnabledProtocols(new String[] {"TLSv1"});
选项 4. 使用 SSLParameters.setProtocols() API
应用程序可以在 SSLParameters 对象中设置协议(protocol),然后通过 SSLSocket.setSSLParameters() 和 SSLEngine.setSSLParameters() 方法将其应用于连接。例如:
// Set TLS 1.0, 1.1 and 1.2 in an SSLParameters object.
sslParameters.setProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
或者
// Set TLS 1.0 only in an SSLParameters object.
sslParameters.setProtocols(new String[] {"TLSv1"});
// Apply the parameters to an SSLSocket object.
sslSocket.setSSLParameters(sslParameters);
// Apply the parameters to an SSLEngine object.
sslEngine.setSSLParameters(sslParameters);
对于客户端应用程序,管理员可能必须从默认启用的协议(protocol)列表中删除 TLS 1.1 或 TLS 1.2,以解决服务器端的 TLS 版本不宽容问题。
关于java - 使用 TLSV1 的 cxf webclient 调用 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071755/