Java 使用jersey-apache-client-1.18的SSL握手失败

Java 使用jersey-apache-client-1.18的SSL握手失败,java,apache,rest,ssl,jersey-client,Java,Apache,Rest,Ssl,Jersey Client,我正在使用jersey apache客户端进行ssl连接。我在验证连接时收到握手错误。下面是握手错误 WRITE: TLSv1 Change Cipher Spec, length = 1 Finished verify_data: { 165, 117, 49, 237, 116, 71, 111, 175, 161, 237, 45, 30 } WRITE: TLSv1 Handshake, length = 48 READ: TLSv1 Alert, length = 2 RECV TL

我正在使用jersey apache客户端进行ssl连接。我在验证连接时收到握手错误。下面是握手错误

WRITE: TLSv1 Change Cipher Spec, length = 1
Finished
verify_data:  { 165, 117, 49, 237, 116, 71, 111, 175, 161, 237, 45, 30 }
WRITE: TLSv1 Handshake, length = 48
READ: TLSv1 Alert, length = 2
RECV TLSv1 ALERT:  fatal, handshake_failure
%% Invalidated:  [Session-1, TLS_DHE_RSA_WITH_AES_128_CBC_SHA]
如果我使用jersey-client-1.13并且没有收到握手错误,那么代码可以正常工作

SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);
final ClientConfig config = new DefaultClientConfig();
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(null, ctx));
Client create = Client.create(config);    
create.resource(targetUrl).post();
由于jersey-client.1.13不支持代理,所以我使用了jersey-apache-client.1.18。在下面的代码中,我使用了DefaultApacheHttpClientConfig,添加了代理支持,并使用ApacheHttpClient创建了客户端

我找不到解决办法。在这两种情况下,证书都是好的

注意:我也尝试了jersey apache客户端,但仍然存在错误


有人发现哪里出了问题吗?提前谢谢

原因是正在为Jersey配置SSL套接字工厂,但是您使用的是Jersey Apache连接器,因此应直接在Apache HttpClient上配置SSL套接字工厂。对于更高版本的Jersey 2.x,这可能可以为您完成,但对于Apache连接器和Jersey 1.x,您需要配置此功能。大概是这样的:

Protocol apacheProtocol = new Protocol("https", socketFactory, 443);
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("your.hostname.org", 443, apacheProtocol);
HttpClient httpClient = new HttpClient();
httpClient.setHostConfiguration(hostConfig);
ApacheHttpClientHandler handler = new ApacheHttpClientHandler(httpClient);
Client client = new ApacheHttpClient(handler);
这里的另一个问题是,如果您在Jersey WebResource集合主机名和端口上使用绝对URI,那么您的自定义SSLSocketFactory的主机配置将被覆盖,HttpClient将默认为至少在Apache客户端3.1上使用JVM的CA证书true

如果这以前对您有效,那么您可能使用了JVM cacerts信任的证书。
实际上,这里的一个选项是使用命令行keytool将您正在使用的证书手动添加到JVMs cacerts文件中,尽管对于某些应用来说,这可能不是一个可接受的解决方案。

基本上,这个问题是由于您的本地JAVA和https站点之间的密码不匹配造成的。您可能需要为JVM安装JCE扩展

用于检查REST API是否支持java

如果它显示为这样,您可以从下载文件。完成后,将JRE的lib/security目录下的两个jar local_policy.jar、US_export_policy.jar替换为下载包中的jar

如果没有,请提供更多详细信息,以便我们详细检查

检查这里的解决方案我只是累了,它为我工作,节省我的一天

Protocol apacheProtocol = new Protocol("https", socketFactory, 443);
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("your.hostname.org", 443, apacheProtocol);
HttpClient httpClient = new HttpClient();
httpClient.setHostConfiguration(hostConfig);
ApacheHttpClientHandler handler = new ApacheHttpClientHandler(httpClient);
Client client = new ApacheHttpClient(handler);