2013-12-16 21 views
2

我向Apache Wink Web服務中的位於雲中的外部資源(我無法將證書添加到JVM)發送請求,並且我知道當我嘗試提出請求時從瀏覽器中,我得到了一個正確的答案。Apache Wink連接到https資源

String serviceURL = "https://someurl&ciUser=user&ciPassword=password"; 

ClientConfig clientConfig = new ClientConfig(); 
clientConfig.setBypassHostnameVerification(true); 

RestClient client = new RestClient(clientConfig); 

Resource resource = client.resource(serviceURL); 

,但我得到以下異常:

[err] org.apache.wink.client.ClientRuntimeException: java.lang.RuntimeException: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake 
[err] at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:240) 
[err] at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:189) 
[err] at org.apache.wink.client.internal.ResourceImpl.get(ResourceImpl.java:302) 

UPDATE

我也試試這個,但得到了同樣的錯誤

String serviceURL = "https://url&ciUser=user&ciPassword=password"; 

//Perform basic http auth 
ClientConfig clientConfig = new ClientConfig(); 
BasicAuthSecurityHandler basicAuthSecurityHandler = new BasicAuthSecurityHandler("user", "password"); 
clientConfig.handlers(basicAuthSecurityHandler); 

RestClient client = new RestClient(clientConfig); 

是否有可能解決這個問題?

回答

0

嘗試以下步驟

  1. 運行與JKS文件我出口HTTPS應用。

  2. 使用瀏覽器查看證書。

  3. 導出並保存成.cer文件

  4. 與Java密鑰工具導入。

這是命令:

keytool -import -trustcacerts -alias localhost -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file "D:/apache tomcat 6/bin/example.cer" 
+0

感謝您的重播。但是我在上面寫道「我不能把證書給JVM」 – Ray

0

雖然這是一個老問題,我想在這裏貼上我的代碼,它應該是有益的,如果你要信任使用表情時的所有證書客戶:

public static void doRequest() { 
    ClientConfig config = new ClientConfig(); 
    RestClient restClient = new RestClient(config); 
    Resource resource = restClient.resource(serviceURL); 
    trustAllCertificates();//trust all certificates before doing the request 
    ClientResponse clientResponse = resource.get(); 
} 
public static void trustAllCertificates() throws RuntimeException { 
    try { 
     TrustManager[] trustManager = new TrustManager[] { 
      new X509TrustManager() { 
       @Override 
       public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
        return null; 
       } 
       @Override 
       public void checkClientTrusted(
        java.security.cert.X509Certificate[] certs, String authType) { 
       } 
       @Override 
       public void checkServerTrusted(
        java.security.cert.X509Certificate[] certs, String authType) { 
       } 
      } 
     }; 

     SSLContext sc = SSLContext.getInstance("TLS");//or SSL, it depends on the certificate 
     sc.init(null, trustManager, new java.security.SecureRandom()); 
     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

    } catch (NoSuchAlgorithmException e) { 
     throw new RuntimeException(e.getMessage()); 
    } catch (KeyManagementException e) { 
     throw new RuntimeException(e.getMessage()); 
    } 
}