这是我在这里发表的第一篇文章,请耐心等待。我有一个WSDL,我使用SOAP UI生成Java客户端。我将客户端插入测试项目并使用客户端访问Web服务。 Web服务需要安全头(这不是wsdl策略的一部分),因此我必须使用Handler将Security Headers添加到创建的SOAP Envelope中。在运行我的程序时,它会抛出以下错误... 当我通过SOAP UI运行相同的SOAP请求时,它似乎处理得很好。 我注意到的另一个有趣的事情是,虽然我在我的客户端中处理它之后在SOAP UI中运行相同的请求(包括安全头的nonce)(在抛出错误之后)它仍然似乎处理正常。但是当我尝试在SOAP UI中运行相同的请求两次时,它会引发一个异常,这个异常就是不能多次使用相同的Nonce值(这是预期的行为)。这让我觉得运行我的客户端后生成的错误甚至没有到达运行Web服务的服务器,否则nonce将被缓存在那里,我将无法在SOAP UI中运行它。我已将我的Handler类方法附加到Error块下面。我倾向于认为安全头很好,因为消息在SOAP UI中处理得很好。 任何帮助表示赞赏。
javax.xml.ws.soap.SOAPFaultException:MustUnderstand标题:{{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}安全性>未被理解 at com.sun.xml.internal.ws.protocol.soap.MUTube.createMUSOAPFaultException(Unknown Source) at com.sun.xml.internal.ws.protocol.soap.ClientMUTube.processResponse(Unknown Source) 在com.sun.xml.internal.ws.api.pipe.Fiber .__ doRun(未知来源) 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(Unknown Source) at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source) 在com.sun.proxy。$ Proxy34.searchDemographics(未知来源) 在com.douglas.client.Client.main(Client.java:50)
public boolean 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;
}
答案 0 :(得分: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.");
}