2012-05-16 90 views
0

我無法弄清楚如何繼續下一個場景。用於HTTPS上WCF的Java客戶端

我有一個WCF服務啓動和運行安全是這樣的:

<binding name="SSLBinding"> 
    <security mode="Transport"> 
    <transport clientCredentialType="None"/> 
    <!--<message clientCredentialType="UserName"/>--> 
    </security> 
</binding> 

我是能夠使虛擬ceritificate在IIS 5.0,所以這個WCF能夠在HTTPS被曝光。

最後使用.Net客戶端,我可以與服務,像這樣的東西傳達:

private void randomMethod 
    { 
     //This is necesary, cause the certificate isn´t valid 
     ServicePointManager.ServerCertificateValidationCallback = 
     new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);    

      RefHTTPS.GestionesServiceClient service = new RefHTTPS.GestionesServiceClient();     

      richTextBox1.Text = service.version();        
    } 

    public static bool IgnoreCertificateErrorHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     return true; 
    } 

問題:當我嘗試做一個Java客戶端,我得到異常上。

有人已經能夠做到這一點,我需要一些指導。

我在網上看了很多帖子,但沒有任何適合我的作品。


編輯(從附加內容合併從OP,被張貼的問題和標記):

這是一個完整的配置(它有一個配置標籤,但沒有顯示):

<?xml version="1.0"?> 
<system.web> 
    <compilation debug="true">   
    </compilation>    
</system.web> 

<system.serviceModel> 
    <bindings> 

<binding name="SSLBinding"> 
    <security mode="Transport"> 
    <transport clientCredentialType="None"/> 
    </security> 
</binding> 

</bindings> 
<serviceHostingEnvironment> 
    <baseAddressPrefixFilters> 
    <add prefix="https://x.x.x.x"/> 
    </baseAddressPrefixFilters> 
</serviceHostingEnvironment> 

<services> 
    <service behaviorConfiguration="WSGestiones.GestionesServiceBehavior" name="WSGestiones.GestionesService"> 
     <endpoint address="https://x.x.x.x/GestionesWS/GestionesService.svc" 
     binding="basicHttpBinding" bindingConfiguration="SSLBinding" contract="WSGestiones.IGestionesService">    
     <identity> 
      <dns value="localhost" />    
     </identity> 
     </endpoint> 
     <endpoint address="https://x.x.x.x/GestionesWS/GestionesService.svc/mex" 
     binding="mexHttpsBinding" contract="IMetadataExchange"> 
     <identity> 
      <dns value="IPADDRESS" /> 
     </identity> 
     </endpoint> 
    </service>  
</services>  

<behaviors> 
    <serviceBehaviors> 
    <behavior name="WSGestiones.GestionesServiceBehavior"> 
     <serviceMetadata httpsGetEnabled="true" httpsGetUrl="https://x.x.x.x/GestionesWS/GestionesService.svc" /> 
     <serviceDebug includeExceptionDetailInFaults="true" />   
    </behavior>   

    </serviceBehaviors> 
</behaviors> 

我能夠將證書添加到我的電腦的信任列表中,我也加入到JDK或任何Java編寫的。

我的一些錯誤是:

Exception in thread "main" javax.xml.ws.WebServiceException: Cannot find 'https://x.x.x.x/GestionesWS/GestionesService.svc?wsdl' wsdl. Place the resource correctly in the classpath. 

HTTP transport error: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present 

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl) 

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.io.IOException: HTTPS hostname wrong: should be <x.x.x.x> 

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: An error occurred when verifying security for the message. 

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 401: Access Denied 

每次我打了一個錯誤,在網上和「固定」的搜索,會出現一個新的....我的「修復」第5天我說「這是一個無限循環「。

所以....而不是試圖解決這些錯誤,我正在考慮「修復邏輯」。

+0

我的猜測是Java不接受證書是有效的。您可能需要將其添加到受信任的證書列表中才能使用。 –

回答

0

我還試圖通過Netbeans連接到服務器,並通過Netbeans爲服務端點提供自簽名證書。當我試圖讓一個新的連接對象,我碰到這個運行時異常:

javax.xml.ws.WebServiceException: java.io.IOException: HTTPS hostname wrong: should be <hostname as in the certificate> 

,我終於找到了關於此異常Metro's documentation部分。他們提供的代碼不幸看起來像只是一個臨時解決方案,但至少看起來你可以解決這個問題。

我修改他們通過創建,我在我的主要方法的頂部,我總是「驗證」我連接到我的服務站點調用靜態方法提供的代碼:

private static void overrideDefaultHostnameVerifier() { 
    //WORKAROUND. TO BE REMOVED. 
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
      new javax.net.ssl.HostnameVerifier() { 
       @Override 
       public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { 
        return true; 
       } 
      }); 
} 

真正的解決方案對於這個問題看起來是在託管服務端點的服務器上獲得真正的頒發證書。