问题
I built the wsdl-client-stub based on xmlbeans. Now I got stuck adding a custom header for authentification using xmlbeans since xmlbeans stubs are lacking the necessary Classes(?)
Actually, the header should look like:
<SOAP-ENV:Header>
<ns2:verifyingToken>
<UserID>9</UserID>
<Token>29438094lkjslfkjlsdkjf</Token>
</ns2:verifyingToken>
</SOAP-ENV:Header>
So I tried as fallback going in between stub and ServiceClient:
ServiceClient sc = stub._getServiceClient();
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement omElement = omFactory.createOMElement(new QName("SOAP-ENV", "Header", "ver"), null);
OMElement omElementVeri = omFactory.createOMElement(new QName("", "verifyingToken", ""), omElement);
OMElement omElementUser = omFactory.createOMElement(new QName("", "UserID", ""), omElementVeri);
omElementUser.setText(""+userid);
OMElement omElementPass = omFactory.createOMElement(new QName("", "Token", ""), omElementVeri);
omElementPass.setText(""+token);
sc.addHeader(omElement);
eclipse is raising errors saying: The method createOMElement(String, OMNamespace) in the type OMFactory is not applicable for the arguments (QName, null) - The constructor QName(String, String, String) is undefined
Does anyone has a hint, what I should fix, to get this to work. I really appreciate your help,
Alex
回答1:
ServiceClient client = stub._getServiceClient();
SOAP11Factory factory = new SOAP11Factory();
OMNamespace SecurityElementNamespace = factory.createOMNamespace("http://schemas.xmlsoap.org/ws/2002/12/secext", "wss");
OMElement usernameTokenEl = factory.createOMElement("UsernameToken", SecurityElementNamespace);
OMElement usernameEl = factory.createOMElement("Username", SecurityElementNamespace);
usernameEl.setText("123");
usernameTokenEl.addChild(usernameEl);
OMElement passwordEl = factory.createOMElement("Password", SecurityElementNamespace);
passwordEl.setText("123");
usernameTokenEl.addChild(passwordEl);
SOAPHeaderBlockImpl block = new SOAP11HeaderBlockImpl("Security", SecurityElementNamespace, factory);
block.addChild(usernameTokenEl);
client.addHeader(block);
来源:https://stackoverflow.com/questions/12997316/adding-authentification-header-to-client-stub-axis2