1

我正在尝试使用这个工具包来测试 Rally 的 webservice api。我们有一个 Rally 的内部设置。我的代码如下所示:

    RallyRestApi restApi = new RallyRestApi (new URI("https://rally"), "userName", "password");
    restApi.setApplicationName("Test");
    restApi.setWsapiVersion(wsapiVersion);

    String workspaceRef = new String("/workspace/11457676");
    String projectRef = new String("/project/11457760");

    String storyFormattedID = "US576";

    QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
    storyRequest.setFetch(new Fetch("FormattedID","Name","Owner"));
    storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", storyFormattedID));
    storyRequest.setWorkspace(workspaceRef);
    storyRequest.setProject(projectRef);
    QueryResponse storyQueryResponse = restApi.query(storyRequest);
    ....

“....”之前的激光线生成异常:javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated。当我在浏览器上手动访问这样的网络服务时工作正常,除了我注意到证书错误:“https://rally/slm/webservice/1.29/defect/10509982”

这个事情谁有经验?谢谢。

4

4 回答 4

1

这绝对是我们在针对具有自签名证书的服务器内部测试工具包时发现的一个问题。看看这个相关的问题:

带有 httpClient 的 SSLPeerUnverifiedException

特别是这个答案:

https://stackoverflow.com/a/9114024/728184

您现在可以通过扩展 RallyRestApi 并配置必要的 SSL 安全覆盖来实现这一点:

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;

import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class OnPremRestApi extends RallyRestApi {

    public OnPremRestApi(URI server, String userName, String password) {
        super(server, userName, password);

        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);
            httpClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", 443, sf));
        } catch (Exception e) {
            //hmm...
        }
    }
}

然后只需在您的代码中使用 OnPremRestApi 的实例而不是 RallyRestApi。

于 2013-01-24T17:27:57.497 回答
1

jar的 2.1 版本开始,工具包允许访问HTTPClient它下面的内容,我们可以告诉HTTPClient忽略无效的证书链并容忍自签名证书以解决SSLPeerUnverifiedException: peer not authenticated异常

当我们实例化 RallyRestApi 时:

String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey);
restApi.setProxy(new URI("http://myproxy.mycompany.com"), "MyProxyUsername", "MyProxyPassword");

HttpClient 我们可以访问 getClient()

这是一个完整的代码示例:

import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.client.HttpClient;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.response.GetResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.scheme.Scheme;


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");  //SET PROXY SETTINS 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"; //USE VALID WORKSPACE OID 
            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();
        }   
    } 
}
于 2014-10-31T17:42:00.533 回答
0

如果它过去可以正常工作,那么您的环境中可能发生了一些变化,特别是与代理有关。

这里记录了 setProxy 方法。如果这确实与代理相关,我希望这会有所帮助。

设置代理

public void setProxy(URI 代理,字符串用户名,字符串密码)

[Set the authenticated proxy server to use. By default no proxy is configured.][2]

Parameters:
    proxy - The proxy server, e.g. new URI("http://my.proxy.com:8000")
    userName - The username to be used for authentication.
    password - The password to be used for authentication.
于 2013-06-17T14:18:12.290 回答
0

我已经长时间使用 RallyRestAPi 实例进行连接,突然它抛出 SSLPeerUnverified 异常,如果我使用你给出的类,错误不会发生。到目前为止,RallyRestAPI 是如何工作的?我用的是 1.0.6 也试过 1.0.7

于 2013-06-17T05:58:52.203 回答