If you have to call the secured webservice with login and password you can do it programmatically by adding the SOAP header with web service handler.
Here is how the header looks like:
<soapenv:Header>
<wss:Security soapenv:mustUnderstand="1" xmlns:wss=" http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wss:UsernameToken>
<wss:Username>username</wss:Username>
<wss:Password>password</wss:Password>
</wss:UsernameToken>
</wss:Security>
</soapenv:Header>
Here is how your code looks like:
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.slf4j.Logger;
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
private Logger log = org.slf4j.LoggerFactory.getLogger(HeaderHandler.class);
private static final String AUTH_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private static final String AUTH_PREFIX = "wss";
public Set<QName> getHeaders() {
try {
SOAPFactory sf = SOAPFactory.newInstance();
SOAPElement se = sf.createElement("Security", AUTH_PREFIX, AUTH_NS);
Set<QName> headers = new HashSet<QName>();
headers.add(se.getElementQName());
return headers;
} catch (SOAPException e) {
e.printStackTrace();
return Collections.emptySet();
}
}
public boolean handleMessage(SOAPMessageContext smc) {
boolean direction = ((Boolean) smc
.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY))
.booleanValue();
try {
String userName = "esb_system_user";
String password = "1234567890";
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPFactory soapFactory = SOAPFactory.newInstance();
// WSSecurity <Security> header
SOAPElement wsSecHeaderElm = soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS);
// create username and password element
SOAPElement userNameElm = soapFactory.createElement("Username", AUTH_PREFIX, AUTH_NS);
userNameElm.addTextNode(userName);
SOAPElement passwdElm = soapFactory.createElement("Password", AUTH_PREFIX, AUTH_NS);
passwdElm.addTextNode(password);
// create username token element
SOAPElement userNameTokenElm = soapFactory.createElement("UsernameToken", AUTH_PREFIX, AUTH_NS);
userNameTokenElm.addChildElement(userNameElm);
userNameTokenElm.addChildElement(passwdElm);
// add child elements to the root element
wsSecHeaderElm.addChildElement(userNameTokenElm);
// create SOAPHeader instance for SOAP envelope
SOAPHeader sh = envelope.addHeader();
// add SOAP element for header to SOAP header object
sh.addChildElement(wsSecHeaderElm);
} catch (SOAPException e) {
e.printStackTrace();
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public void close(MessageContext smc) {
}
}
To use the handler you have to write the handler resolver:
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
import java.util.ArrayList;
import java.util.List;
public class HeaderHandlerResolver implements HandlerResolver {
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
}
}
Here is how you can use your client:
SomeService service = new SomeService ();
service.setHandlerResolver(new HeaderHandlerResolver());
IService fileService = service.getIServiceHttpPort();
For the reference see the following note:
http://www-01.ibm.com/support/docview.wss?uid=swg21675464
Комментариев нет:
Отправить комментарий