2013-01-24 47 views
1

我想用這個工具包來測試拉力賽的webservice api。我們有一個拉力賽的內部設置。我的代碼如下所示:集會休息api java工具包sslpeerunverifiedexception:同行未驗證

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:同行不被認證。 當我手動訪問這樣的web服務上的瀏覽器工作正常,除了我注意到有證書錯誤: 「HTTPS://rally/slm/webservice/1.29/defect/10509982」

有沒有人有這樣的經歷? 謝謝。

回答

1

這絕對是我們在使用自簽名證書對內部服務器測試工具包時發現的問題。看看此相關的問題:

SSLPeerUnverifiedException with httpClient

,具體這樣的回答:

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... 
     } 
    } 
} 

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

您可以通過擴展RallyRestApi並配置必要的SSL安全覆蓋實現這個今天

然後,只需使用OnPremRestApi的一個實例,而不是RallyR estApi在你的代碼中。

+0

工程。非常感謝。 – user2008162

0

我已經使用了RallyRestAPi實例很長一段時間的連接,突然它拋出SSLPeerUnverified異常,如果我使用你給出錯誤的類不會發生。 RallyRestAPI如何工作到現在?我正在使用1.0.6也嘗試過1.0.7

0

如果它曾經在過去工作過,或許你的環境中發生了一些變化,特別是與代理有關。

有setProxy方法記錄here。如果這確實與代理有關,我希望這有助於。

setProxy

public void setProxy(URI proxy, String userName, String password)

[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. 
1

2.1 version of the jar啓動工具包允許訪問其下的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"); 

我們可能會與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(); 
     } 
    } 
}