为了回答我自己的问题,我决定使用手持者。所以我创造了一个类似于上述交易所供应商的SOAPHandler:
public class MyAuthenticationHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext context) {
final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outInd.booleanValue()) {
try {
UserNamePasswordPair userNamePasswordPair = getAuthorization(); // Method to retrieve credentials from somewhere
context.put(BindingProvider.USERNAME_PROPERTY, userNamePasswordPair.getUsername());
context.put(BindingProvider.PASSWORD_PROPERTY, userNamePasswordPair.getPassword());
} catch (final Exception e) {
return false;
}
}
return true;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
logger.error("error occurred when getting auth.");
return false;
}
@Override
public void close(MessageContext context) {
logger.debug("closing handler for auth...");
}
@Override
public Set<QName> getHeaders() {
return null;
}
}
创建了处理器解析器, 将解析器添加到链上 :
public class MyHandlerResolver implements HandlerResolver {
private List<Handler> chain;
public MyHandlerResolver() {
chain = new ArrayList<Handler>();
chain.add(new MyAuthenticationHandler();
}
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
return chain;
}
}
然后在春天,就把事情搞成这样:
<bean id="myJAXWSClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="Interface to implement"/>
<property name="wsdlDocumentUrl" value="classpath:/wsdl/theWsdl.wsdl"/>
<property name="namespaceUri" value="namespace"/>
<property name="serviceName" value="ServiceName"/>
<property name="endpointAddress" value="/endpoint"/>
<property name="handlerResolver" ref="myHandlerResolver"/>
</bean>
<bean id="myHandlerResolver" class="com.mystuff.ExchangeHandlerResolver"/>