我正在尝试使用来自Netbeans的gSoap生成的wsdl。 web服务要求传入UserNameToken。当我使用SoapUI中的wsdl(有效)时,它会发送:
<wsse:Username>myname</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">mypass</wsse:Password>
但是,当我使用处理程序(由How do I add a SOAP Header using Java JAX-WS引用)从Netbeans尝试这样的时候:
String prefix = "wsse";
String uri = "http://...wsssecurity...";
SOAPElement securityElem = factory.createElement("Security",prefix,uri);
SOAPElement UserNametokenElem = factory.createElement("UserNameToken",prefix,uri);
SOAPElement UsernameElem = factory.createElement("wsse:Username");
UsernameElem.addTextNode("myname");
SOAPElement PasswordElem = factory.createElement("Password");
PasswordElem.addTextNode("mypass");
UserNametokenElem.addChildElement(UsernameElem);
UserNametokenElem.addChildElement(PasswordElem);
securityElem.addChildElement(UserNametokenElem);
SOAPHeader header = envelope.addHeader();
header.addChildElement(securityElem);
它会像这样生成soap标头:
<wsse:Security xmlns:wsse="http://...wsssecurity...">
<wsse:UserNameToken xmlns:wsse="http://...wsssecurity...">
<Username xmlns="">myname</Username>
<Password xmlns="">mypass</Password>
</wsse:UserNameToken>
</wsse:Security>
导致web服务以身份验证错误进行回复。
我的问题是:
答案 0 :(得分:6)
我最终成功使用了wss4j,做了类似的事情:
SOAPPart soappart = message.getSOAPPart();
SOAPEnvelope envelope = soappart.getEnvelope();
SOAPHeader header = envelope.getHeader();
WSSecHeader wsheader = new WSSecHeader();
wsheader.insertSecurityHeader(soappart);
WSSecUsernameToken token = new WSSecUsernameToken();
token.setPasswordType(WSConstants.PASSWORD_DIGEST);
token.setUserInfo("myuser", "mypass");
token.build(soappart, wsheader);
wss4j有一些嵌套的依赖关系,所以要小心。