Java 我在使用jersey客户端使用HTTPS调用api时遇到了握手失败的错误。我已关闭服务器证书有效性检查
当使用jersey客户端使用HTTPS调用api时,我遇到了错误握手失败。 如果站点作为HTTP托管,那么它工作正常。 使用HTTPS时,我已关闭服务器证书有效性检查Java 我在使用jersey客户端使用HTTPS调用api时遇到了握手失败的错误。我已关闭服务器证书有效性检查,java,api,https,ssl-certificate,sslhandshakeexception,Java,Api,Https,Ssl Certificate,Sslhandshakeexception,当使用jersey客户端使用HTTPS调用api时,我遇到了错误握手失败。 如果站点作为HTTP托管,那么它工作正常。 使用HTTPS时,我已关闭服务器证书有效性检查 @Test public void GetMember2(){ try{ System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); Date today1 = new Date(); // default window
@Test
public void GetMember2(){
try{
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
Date today1 = new Date(); // default window is 1 hour
String stringToday1 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(today1);
Client client = Client.create(configureClient());
WebResource webResource = client.resource("https://test.abcd.net/Members/72771583886/promotions?startDate=12122015");
ClientResponse response = webResource.accept("application/xml")
.type("application/xml")
.header("Date", stringToday1)
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
public static ClientConfig configureClient() {
TrustManager[ ] certs = new TrustManager[ ] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(null, certs, new SecureRandom());
} catch (java.security.GeneralSecurityException ex) {
}
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
ClientConfig config = new DefaultClientConfig();
try {
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
},
ctx
));
} catch(Exception e) {
}
return config;
}
错误:com.sun.jersey.api.client.clienthandler异常:
javax.net.ssl.SSLHandshakeException:收到致命警报:
握手失败
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
位于com.sun.jersey.api.client.client.handle(client.java:652)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)位于
com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
在
com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)
位于Frameworks.Prototype2.getLoyaltMember2(Prototype2.java:106)
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于
位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于
java.lang.reflect.Method.invoke(未知源)
握手可能由于各种原因而失败,不仅仅是因为证书验证问题。 可以是:
- 不共享相同的密码套件
- 不共享SSL版本
- 证书验证
- 更改TLS版本的意图
- 其他
此链接也可能有帮助:暂时打开Java网络库的调试,以查看您收到的SSL错误类型:
-Djavax.net.debug=all
从oracle网站下载jce_policy-8.zip,并将其解压缩以获得以下两个JAR文件
local_policy.jar US_export_policy.jar
在JVM中更新上述两个策略,即在我的例子中/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/security
当我使用HTTPS连接调用api时,下面的代码在C#/Visual Studio中非常有效。我在Java中找不到类似的东西//关闭服务器证书有效性检查ServicePointManager.ServerCertificateValidationCallback=delegate(对象s、X509Certificate证书、X509Chain链、SslPolicyErrors SslPolicyErrors){return true;};ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11@用户6332513,好的,只需使用wireshark或其他工具了解SSL警报的原因。它是什么警报类型。当我使用HTTPS连接调用api时,以下代码在C#/VisualStudio中非常有效。我在Java中找不到类似的东西//关闭服务器证书有效性检查ServicePointManager.ServerCertificateValidationCallback=delegate(对象s、X509Certificate证书、X509Chain链、SslPolicyErrors SslPolicyErrors){return true;};ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;您也可以从较旧的JDK版本中获得此错误(可能来自JDK中包含的不同/较旧的TLS支持)。例如,使用1.7执行的相同代码会出现此错误,但1.8不会。