2014-04-09 93 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