2013-11-26 98 views
5

我正在使用java中的web服務。此服務正在使用UserNameToken,時間戳和簽名進行消息級安全性。我已經給SoapUI項目文件(xml)嘗試從服務中獲取數據。一切工作正常在SoapUI。在java和SOAPUI中使用Web服務

現在,當我試圖使用相同的soapUI文件生成工件時,我收到一條消息"Receiver Policy falsified"。爲什麼我可以連接到soapUI中的服務,但是我不能在java中?

所以我發現我沒有使用傳輸層安全性發送密鑰庫。我如何在SOAP請求中發送此消息。我在SSL設置中的SOAP UI中完成了類似的設置,並且可以在此工作。

這是我在Java

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

    String url = "https://abc.org/test/ApplicantInformationService"; // Test System Web Service URL 

SSLSocketFactory sslSocketFactory = getSSLSocketFactory(); 

conn = urlOnly.openConnection(); 
if (conn instanceof HttpsURLConnection) { 
    ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory); 
    ((HttpsURLConnection) conn).setRequestMethod("POST"); 
} 

conn.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");  
conn.setDoOutput(true); 
SOAPMessage message = createSOAPRequest(user); 
ByteArrayOutputStream os1 = new ByteArrayOutputStream(); 
message.writeTo(os1); 
String requestXml = new String(os1.toByteArray()); 

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); 
out.write(requestXml); 
out.flush(); 
if(out != null){ 
    out.close(); 
}    
String line = "";     
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String responseNewWay = ""; 
while ((line = in.readLine()) != null) { 
    responseNewWay = responseNewWay + line; 
} 
//SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(user), url); 
//soapConnection.close(); 
//ByteArrayOutputStream os = new ByteArrayOutputStream(); 
//soapResponse.writeTo(os); 
//String responseXml = new String(os.toByteArray()); 

SOAP請求的代碼和SSLFactory代碼是這樣的

private static SSLSocketFactory getSSLSocketFactory() 
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, IOException, KeyManagementException{ 
    SSLSocketFactory sslsf = null;     
    String keyStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";   

     String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();   
    String keyStoreType = "JKS"; 
     String keyStorePassword = "mypassword"; 
     String trustStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";   

     String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath(); 
     String trustStorePassword = "mypassword"; 
     String alias = "clientcertificate"; 

     Properties systemProps = System.getProperties(); 
     systemProps.put("javax.net.ssl.keyStore", keyStorePath); 
     systemProps.put("javax.net.ssl.keyStorePassword", keyStorePassword); 
     systemProps.put("javax.net.ssl.keyStoreType", keyStoreType); 

     systemProps.put("javax.net.ssl.trustStore", trustStorePath); 
     systemProps.put("javax.net.ssl.trustStoreType", "JKS"); 
     systemProps.put("javax.net.ssl.trustStorePassword", trustStorePassword); 
     System.setProperties(systemProps); 

     KeyManager[] keyManagers = createKeyManagers(keyStoreFileName,keyStorePassword, alias); 
     TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword); 
     sslsf = initItAll(keyManagers, trustManagers); 

     return sslsf; 
    } 

和錯誤SOAP響應我得到是這樣

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> 
    <soapenv:Body> 
     <soapenv:Fault> 
      <soapenv:Code> 
       <soapenv:Value>soapenv:Receiver</soapenv:Value> 
      </soapenv:Code> 
      <soapenv:Reason> 
       <soapenv:Text xml:lang="en-US">Policy Falsified</soapenv:Text> 
      </soapenv:Reason> 
      <soapenv:Role>https://abc.org/test/ApplicantInformationService</soapenv:Role> 
      <soapenv:Detail> 
       <l7:policyResult 
        status="Service Not Found. The request may have been sent to an invalid URL, or intended for an unsupported operation." xmlns:l7="http://www.layer7tech.com/ws/policy/fault"/> 
      </soapenv:Detail> 
     </soapenv:Fault> 
    </soapenv:Body> 

現在我得到這個錯誤

Server returned HTTP response code: 500 for URL: https://abc.org/test/ApplicantInformationService 
+0

您是否定義了任何策略集? –

+0

不,如果我使用我通過javaui代碼創建的請求,在soapui中,當我應用「身份驗證」時也可以工作。所以我不理解我將如何在我的java代碼中應用身份驗證。 – yogsma

+0

什麼是您的應用程序服務器? – user1807337

回答

2

也許你必須附加「WSDL?」在URL字符串的結尾:

String url = "https://abc.org/test/ApplicantInformationService?wsdl"; 
2

能否請您發表您的createSOAPRequest方法的代碼?我想您對請求沒有任何證書問題,因爲您從服務器接收到有效的SOAP響應,看起來您的安全性頭文件有問題,請檢查您的請求SOAP消息是否爲Timestamp元素,也許您發送的是創建的或到期標籤和服務器拒絕您的請求。