2014-04-09 94 views
1

這是我第一篇文章,所以請耐心等待。我有一個WSDL,並使用SOAP UI來生成Java客戶端。我將客戶端插入測試項目,並使用客戶端訪問Web服務。 Web服務期望安全頭(不屬於wsdl策略的一部分),所以我不得不使用Handlers來將安全頭添加到已創建的SOAP Envelope。在運行我的程序時,會拋出以下錯誤... 當我通過SOAP UI運行相同的SOAP請求時,它似乎處理得很好。 我注意到的另一個有趣的事情是,雖然我在客戶端處理它後(在拋出錯誤之後)在SOAP UI中運行相同的請求(包括安全頭的隨機數),但它似乎仍然處理得很好。但是,當我嘗試在SOAP UI中運行相同的請求兩次時,它會拋出一個異常,這種異常不能一次使用相同的Nonce值(這是預期的行爲)。這使我認爲運行我的客戶端後生成的錯誤甚至沒有到達運行Web服務的服務器,否則nonce會被緩存到那裏,我將無法在SOAP UI中運行它。我在Error塊下面附加了Handler類的方法。我傾向於認爲安全頭是好的,因爲消息在SOAP UI中處理得很好。 任何幫助表示讚賞。SOAP UI生成JAVA客戶端拋出MustUnderstandHeaders不明白錯誤

javax.xml.ws.soap.SOAPFaultException:mustUnderstand屬性標題:[{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}安全]在com.sun.xml.internal.ws.protocol.soap.MUTube.createMUSOAPFaultException(未知來源) 不被理解 at com.sun.xml.internal.ws.protocol.soap.ClientMUTube.processResponse(Unknown Source) at com.sun.xml.internal.ws.api.pipe.Fiber .__ doRun(Unknown Source) at com.sun .xml.internal.ws.api.pipe.Fiber._doRun(Unknown Source) at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Unknown Source) at com.sun.xml.internal .ws.api.pipe.Fiber.runSync(Unknown Source) at com.sun.xml.internal.ws.client.Stub.process(Unknown Source) at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(Unknown Source) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source) at com。 sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(未知源) at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(未知源) at com.sun.proxy。 $ Proxy34.searchDemographics(來源不明) 在com.douglas.client.Client.main(Client.java:50)

公共布爾的handleMessage(SOAPMessageContext SMC){

Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

    if (outboundProperty.booleanValue()) { 

     SOAPMessage message = smc.getMessage(); 


     try { 
      //message.writeTo(System.out); 
      //System.out.println("\n"); 
      String nonce = getNonce(); 
      String password1 = "password01"; 
      SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
      String messageCrTime = ft.format(new Date()); 
      String passwordDigest = SHAsum(nonce, password1, messageCrTime); 

      SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope(); 
      SOAPHeader header = envelope.addHeader(); 

      SOAPElement security = 
        header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 

      SOAPElement usernameToken = 
        security.addChildElement("UsernameToken", "wsse"); 
      usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); 

      SOAPElement username = 
        usernameToken.addChildElement("Username", "wsse"); 
      username.addTextNode("USERNAME"); 

      SOAPElement password = 
        usernameToken.addChildElement("Password", "wsse"); 
      password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"); 
      password.addTextNode(passwordDigest); 

      SOAPElement nonceElem = 
        usernameToken.addChildElement("Nonce", "wsse"); 
      //password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); 
      nonceElem.addTextNode(Base64.encodeBytes(nonce.getBytes())); 

      SOAPElement created = 
        usernameToken.addChildElement("Created", "wsu"); 
      //password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); 
      created.addTextNode(messageCrTime); 

      //Print out the outbound SOAP message to System.out 
      message.writeTo(System.out); 
      System.out.println(""); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } else { 
     try { 

      //This handler does nothing with the response from the Web Service so 
      //we just print out the SOAP message. 
      SOAPMessage message = smc.getMessage(); 
      message.writeTo(System.out); 
      System.out.println(""); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 


    return outboundProperty; 

} 

回答

1

發現瞭解決方案。客戶端拋出這些錯誤的原因是因爲當它從Web服務接收到響應時,mustUnderstand被設置爲'1'。現在,由於WSDL策略不包含安全組件,客戶端不知道如何處理它。解決方法是在HeaderHandler.java類中添加以下方法。

public Set getHeaders() { 
     final QName securityHeader = new QName( 
       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", 
       "Security", "wsse"); 

     final HashSet<QName> headers = new HashSet<QName>(); 
     headers.add(securityHeader); 
     return headers; 

     //throw new UnsupportedOperationException("Not supported yet."); 
    } 
+1

謝謝@vineets ... !!它幫了很多...... !!! – Harshit