Rally rest API的SSLHandshakeException

iqjalb3h  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(92)

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.
我如何解决这个问题?谢谢。

qvtsj1bj

qvtsj1bj1#

你写了你想找到一种方法来允许所有证书,并使用HttpClient和Rally Rest Toolkit for Java。以下是如何从restApi访问HttpClient:

HttpClient client = restApi.getClient();

下面是一个信任所有证书的示例。g.自签名证书:

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();
        }   
    } 
}

相关问题