我正在尝试连接到现有的SOAP服务。我给出了一个wsdl,并由此通过Apache Axis2 XMLBeans创建了Java类。
但是现在我必须用WS-Security头扩展我的自写客户端发送的消息。我该怎么做?
我已经找到了Apache项目,但是找不到任何解决方案来扩展从我的Java类创建的具有这样一个头的消息。我只能找到机会建立一个服务的WS安全(在webapp文件夹,等等)。
我对你的回答很兴奋!
谢谢你的帮助!
发布于 2015-01-19 08:16:37
我解决了我的问题。我想与你分享我的解决方案,希望任何人都能需要它!
如前所述,我使用Apache Axis2 XMLBeans (http://axis.apache.org/axis2/java/core/docs/quickstartguide.html#clientxmlbeans)创建了一个wsdl文件。
在此之后,我需要添加一个WS Security头,它应该如下所示:
<wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username><YOUR USERNAME></wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><YOUR PASSWORD></wsse:Password>
</wsse:UsernameToken>
</wsse:Security> 我编程解决了这个问题:
在存根中有一个SOAP方法(一个没有,另一个带有callbackHandler),您想要调用这个方法来使用web服务。在此方法中,存在一个名为_messageContext的变量。这是您可以到达标头的地方:_messageContext.getEnvelope().getHeader()返回一个SOAPHeader实例。通过这个实例,我从类addSecurityToHeader调用HeaderAddery方法。
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HeaderAdder {
public static void addSecurityToHeader(
org.apache.axiom.soap.SOAPHeader header) {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespaceWSSE = factory
.createOMNamespace(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"wsse");
OMElement element = factory.createOMElement("Security", namespaceWSSE);
OMAttribute attribute = factory.createOMAttribute("mustUnderstand",
null, "1");
element.addAttribute(attribute);
header.addChild(element);
OMElement element2 = factory.createOMElement("UsernameToken",
namespaceWSSE);
OMNamespace namespaceWSU = factory
.createOMNamespace(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
"wsu");
attribute = factory.createOMAttribute("Id", namespaceWSU,
"UsernameToken-1");
element2.addAttribute(attribute);
element.addChild(element2);
OMElement element3 = factory.createOMElement("Username", namespaceWSSE);
element3.setText("<YOUR USERNAME>");
OMElement element4 = factory.createOMElement("Password", namespaceWSSE);
attribute = factory
.createOMAttribute(
"Type",
null,
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
element4.setText("<YOUR PASSWORD>");
element2.addChild(element3);
element2.addChild(element4);
}
}这样,认证就成功了,我再也没有拒绝的回应了。
如果你对此有任何疑问,请告诉我!
亲切的问候!
https://stackoverflow.com/questions/27972555
复制相似问题