2016-03-15 95 views
1

我目前正在使用http服務的web服務!我被要求更改(使用)https而不是調用此webservice!使用https調用Web服務

我使用日食開普勒和JBoss EAP6.1

我,我要創建一個密鑰庫和編輯的server.xml文件在互聯網上找到。 事情是,我無法找到這個JBOss版本的XML文件[我有一個standalone.xml文件是一樣的嗎? ] 和密鑰存儲的生成其中我必須這樣做嗎? 謝謝你的安澤! 如果我在錯誤的方式,你會請重定向我?

再次感謝!

回答

1

獲取HTTPS url的證書。 (您可以在瀏覽器中輸入URL,然後從瀏覽器證書安裝位置提取證書)。在此之後,將此證書添加到JBOSS服務器使用的應用程序的JRE中。很可能這將是您在系統環境中提供的JRE。您可以通過谷歌獲得如何在密鑰庫中安裝證書。可能會這樣做。

0

您正在通過https調用遠程web服務,對不對?

好吧,你可以導入遠程服務的證書中的密鑰庫(大量有關指南,看看this other question爲例)

OR

您可以繞過整個HTTPS證書的事(遠程調用之前推出這個靜態方法):

/** 
* Bypassing SSL certificate check 
* 
* @throws Exception 
*/ 
public static void doTrustToCertificates() throws Exception { 
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
    TrustManager[] trustAllCerts = new TrustManager[]{ 
     new X509TrustManager() { 
      @Override 
      public X509Certificate[] getAcceptedIssuers() { 
       return null; 
      } 

      @Override 
      public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { 
      } 

      @Override 
      public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { 
      } 
     } 
    }; 

    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    HostnameVerifier hv = new HostnameVerifier() { 
     @Override 
     public boolean verify(String urlHostName, SSLSession session) { 
      if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) { 
       logger.warn("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'."); 
      } 
      return true; 
     } 
    }; 
    HttpsURLConnection.setDefaultHostnameVerifier(hv); 
} 
0

除了回答@Rahul的,你可以導入在命令提示符下使用以下命令證書(.cer)文件的Windows操作系統:

(假設你已經設置所需的Java路徑)

keytool -importcert -file <path of certificate>\<YourCertificateName>.cer -keystore D:\java\jdk1.7.0_40\jre\lib\security\cacerts -alias <certificateAliasName> -storepass <Password> 

通常默認<password>是 '的changeit'。

如果web服務用於第三方客戶端,那麼您可以使用HttpClient進行交互。我不確定您使用該web服務執行哪種操作。我假設你想發送一些XML到該URL。你可以參考下面的代碼:

  HttpPost httppost = new HttpPost(url); 
      CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
      credentialsProvider.setCredentials(AuthScope.ANY, 
        new UsernamePasswordCredentials(username, password)); 
      CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); 
      StringEntity entity = null; 
      try { 
       entity = new StringEntity(xmlToSend); 
      } catch (UnsupportedEncodingException e) { 
       LOG.error("Unsupported Encoding ", e); 
      } 
      entity.setContentType("text/xml"); 
      httppost.setEntity(entity); 
      try{ 
       CloseableHttpResponse response = client.execute(httppost); 
       returnCode = response.getStatusLine().getStatusCode(); 
       EntityUtils.consume(entity); 
       LOG.debug("HttpResponse :" + EntityUtils.toString(response.getEntity())); 
      }catch(IOException e){ 
       LOG.error("Error occured while sending the xml"); 
      } 
+0

我真的不應該從哪裏開始我應該配置服務器的xml文件,如果是的話哪一個?如果我必須創建或導入certificat從哪裏到哪裏我應該這樣做....最後,我正在處理的webservice是爲另一家公司,所以如果我將證書添加到jdk目錄那怎麼回事爲公司工作? – Hopeful

+0

我以前遇到類似的情況,所以在你的情況下,如果你的webservice正在爲其他公司工作,那麼理想情況下你不應該導入任何證書(提供客戶端的jdk/jre是可以訪問的)。我使用HttpClient 4.x作爲我必須以xml的形式向客戶端(其他公司)發送一些數據。您是否擁有該公司的https URL的SLA?我的意思是那裏有任何可以使用你的web服務的公司的機制?如果是,那麼你可以使用httpclient(還有其他的API!)。讓我知道你是否需要一些關於代碼的幫助。 – Suyash