Recently I had the "chance" to consume an old school WebService in good old SOAP on top of that it was a C# WebService using the dataset construct which is in itself a world of pain... but we will not talk about that here
A more easy thing to tackle is the NTLM authentication, below is how I implemented it using Spring and Spring boot :
1. Define a set of custom properties in the application.yaml file
1 2 3 4 5 |
ws: username: "myusername" password: "mypassword" authentication: "NTLM" |
2. Map the properties in application.yaml to a Java class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.stereotype.Component;@Component@EnableConfigurationProperties@ConfigurationProperties(prefix = "app")@Datapublic class YAMLConfig { private Ws ws; @Data public static class Ws { private String baseUrl; private String username; private String password; private String authentication; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.transport.http.HTTPConduit;import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;import org.springframework.stereotype.Service;import org.w3c.dom.Element;import javax.xml.bind.JAXBContext;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.stream.Collectors;//Injection and other properties omitted for brevitypublic void consume(){ JaxWsProxyFactoryBean factory1 = new JaxWsProxyFactoryBean(); factory1.setServiceClass(EntityInformationSoap.class); factory1.setAddress(yamlConfig.getWs().getBaseUrl()); factory1.setUsername(yamlConfig.getWs().getUsername()); factory1.setPassword(yamlConfig.getWs().getPassword()); EntityInformationSoap service = (EntityInformationSoap) factory1.create(); Client client = ClientProxy.getClient(service); HTTPConduit conduit = (HTTPConduit) client.getConduit(); conduit.getAuthorization().setAuthorizationType(yamlConfig.getWs().getAuthentication()); conduit.getAuthorization().setUserName(yamlConfig.getWs().getUsername()); conduit.getAuthorization().setPassword(yamlConfig.getWs().getPassword()); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setConnectionTimeout(36000); httpClientPolicy.setAllowChunking(false); conduit.setClient(httpClientPolicy);} |