2016-03-01 40 views
0

我是Jboss的新手,並且在我的jboss代碼中使用rest rest easy client進行連接。下面是代碼 -如何繞過JBOSS中的SSL證書問題REST ClientRequest

--- 
import org.jboss.resteasy.client.ClientRequest; 
import org.jboss.resteasy.client.ClientResponse; 
--- 
public String login() throws Exception { 
--- 
    String URL = "https://IP//service/perform.do?operationId=XXXXX"; 
    ClientRequest restClient = new ClientRequest(URL); 
    restClient.accept(MediaType.APPLICATION_JSON); 
    restClient.body(MediaType.APPLICATION_JSON, hmap); 
    ClientResponse <String> resp = restClient.post(String.class); 
    if (resp.getStatus() != 201) { 
     throw new RuntimeException("Failed : HTTPS error code : " + resp.getStatus()); 
    } 
    BufferedReader br = new BufferedReader(new InputStreamReader(
     new ByteArrayInputStream(resp.getEntity().getBytes()))); 
    String output; 
    System.out.println("Output from Server .... \n"); 
    while ((output = br.readLine()) != null) { 
     System.out.println(output); 
    } 
    return output; 
} 

雖然啓用SSL服務器連接,獲取證書錯誤「HTTP狀態500 - org.jboss.resteasy.spi.UnhandledException:javax.net.ssl.SSLException:主機名的證書沒」不匹配「。

我們目前無法更換證書,但有什麼方法可以信任任何證書?我搜索了很多帖子,但沒有任何幫助。

任何人都可以告訴我什麼是這個問題的解決方案。

+0

我得到了解決 –

回答

0

我得到了下面的解決方案:

public static void trustAllCertificates() throws NoSuchAlgorithmException, KeyManagementException 
    { 
     // Create a trust manager that does not validate certificate chains 
     TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { 
       public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
        return null; 
       } 
       public void checkClientTrusted(X509Certificate[] certs, String authType) { 
       } 
       public void checkServerTrusted(X509Certificate[] certs, String authType) { 
       } 
      } 
     }; 

     // Install the all-trusting trust manager 
     SSLContext sc = SSLContext.getInstance("SSL"); 
     sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

     // Create all-trusting host name verifier 
     HostnameVerifier allHostsValid = new HostnameVerifier() { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }; 

     HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 
    }