rally rest api java toolkit sslpeerunverifiedexception:对等未验证
我试图使用这个工具包来测试Rally的webservice api。我们有一个拉力赛的内部设置。我的代码如下所示:rally rest api java toolkit sslpeerunverifiedexception:对等未验证,rally,Rally,我试图使用这个工具包来测试Rally的webservice api。我们有一个拉力赛的内部设置。我的代码如下所示: RallyRestApi restApi = new RallyRestApi (new URI("https://rally"), "userName", "password"); restApi.setApplicationName("Test"); restApi.setWsapiVersion(wsapiVersion); String w
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未经身份验证。
当我在浏览器上像这样手动访问Web服务时,工作正常,但我注意到有证书错误:
"https://rally/slm/webservice/1.29/defect/10509982"
有人有这方面的经验吗?
谢谢。这肯定是我们在针对具有自签名证书的服务器对工具包进行内部测试时发现的问题。查看此相关问题: 特别是这个答案: 现在,您可以通过扩展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。我在连接中使用了RallyRestApi实例很长时间,突然它抛出了SSLPeerUnverified异常,如果我使用您给定的类,则不会发生错误。到目前为止,RallyRestAPI是如何工作的?我使用的是1.0.6,也尝试了1.0.7,如果它在过去工作过,可能您的环境中发生了一些变化,特别是与代理有关 有一个setProxy方法记录在案。如果这确实与代理相关,我希望这会有所帮助 setProxy public void setProxy(URI代理、, 字符串用户名, 字符串(密码) 从工具包开始,允许访问它下面的
HTTPClient
,我们可以告诉HTTPClient
忽略无效的证书链并容忍自签名证书,以便解决SSLPeerUnverifiedException:对等未验证的
异常
当我们实例化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");
我们可以使用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();
}
}
}
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();
}
}
}