出现SSLHandshakeException与拉力REST API
问题描述:
在API查询功能与以下异常失败:出现SSLHandshakeException与拉力REST API
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
为了解决这个例外,我手动下载证书并将其导入到cacerts中,一切工作为例外。但此证书的有效期限已设定为几天,因此此解决方案无法生存。
为了测试目的,我创建了一个允许所有证书的信任策略,但是我没有找到一种方法将它与Rest Api集成。我正在使用HttpClient 4.4。
我该如何解决这个问题?谢谢。
答
你写道,你想找到一种方法来允许所有的证书,并使用Rally Rest Toolkit for Java的HttpClient。以下是您如何从restApi访问HttpClient的示例:
HttpClient client = restApi.getClient();
以下是一个信任所有证书的示例,例如,自签名证书:
public class ConnnectionTestWithHTTPClient {
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String applicationName = "Connnection Test With HTTPClient";
RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey);
restApi.setApplicationName(applicationName);
//restApi.setProxy(new URI("http://myproxy.mycompany.com"), "MyProxyUsername", "MyProxyPassword"); //YOUR PROXY SETTINGS HERE
HttpClient client = restApi.getClient();
try {
SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
public boolean isTrusted(X509Certificate[] certificate, String authType)
throws CertificateException {
//trust all certs
return true;
}
}, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sf));
String workspaceRef = "/workspace/12345";
GetRequest getRequest = new GetRequest(workspaceRef);
GetResponse getResponse = restApi.get(getRequest);
System.out.println(getResponse.getObject());
} catch (Exception e) {
System.out.println(e);
} finally {
restApi.close();
}
}
}
+0
这对我有效。谢谢! – Tontodoin
是否有原因导致您无法使用Rally Rest Toolkit for Java? https://github.com/RallyTools/RallyRestToolkitForJava –
我正在使用Rally Rest Toolkit for java。这个问题出现在api的查询函数中。在此阶段获取此SSLhandshakeException。我可以通过在可信密钥库中添加证书来临时解决此问题,但有效性设置为小范围。 – Tontodoin