By default Axis is not configured to support NTLM Authentication. To enable NTLM Authentication Axis must be configured to use the Jakarta Commons HTTPClient library. NTLM Authentication is often required to use Windows based web services.
There are a few different methods for accomplishing this, but this is probably the quickest and simplest technique:
- Download and copy both the Jakarta Commons HTTPClient and Codec JAR files into your project and add them to the classpath.
- Copy and paste the following method into your own class; append it to the end of your pre-existing class or create a separate utility class:
public static org.apache.axis.EngineConfiguration getEngineConfiguration() { java.lang.StringBuffer sb = new java.lang.StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); sb.append("<deployment name=\"defaultClientConfig\"\r\n"); sb.append("xmlns=\"http://xml.apache.org/axis/wsdd/\"\r\n"); sb.append("xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\r\n"); sb.append("<globalConfiguration>\r\n"); sb.append("<parameter name=\"disablePrettyXML\" value=\"true\"/>\r\n"); sb.append("<parameter name=\"enableNamespacePrefixOptimization\" value=\"true\"/>\r\n"); sb.append("</globalConfiguration>\r\n"); sb.append("<transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.CommonsHTTPSender\"/>\r\n"); sb.append("<transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\r\n"); sb.append("<transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>\r\n"); sb.append("</deployment>\r\n"); return new org.apache.axis.configuration.XMLStringProvider(sb.toString()); }
Note: It's probably a good idea to separate the XML markup from the Java code (move XML markup to separate file). I just left the two joined together for the sake of simplicity / laziness since it eliminated one extra step from this blog posting.
- When you create an instance of your web service locator class pass the getEngineConfiguration() method as an argument in the constructor like this:
MyWebServiceStub stub = (MyWebServiceStub)new MyWebServiceLocator(getEngineConfiguration());
-
Set your user name and password like this:
stub.setUsername("<DOMAIN>\\<USER_NAME>"); //Must use this format stub.setPassword("<PASSWORD>");
For more information see the following links:
Is this true for Axis2 also?
ReplyDeleteSorry, I am not sure I have not worked with Axis2 yet.
ReplyDeleteThis is great. It worked well for 2010 SSRS. Thank you for the post !
ReplyDeleteI got the 401 error as well. did you resolve it? what's the DOMAIN should be
ReplyDeleteGreat! thank you very much, I spent all day trying to connect to a ws with ntlm with no luck until I found this post!
ReplyDeleteThank you so much!
ReplyDeleteDid anyone find the solution for the 401 Unauthorized error? This works great with one user and password that i originally set it up with. However, now when I try to change to any other user and password, I kept getting this error.
ReplyDeleteDec 4, 2015 9:42:19 AM org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
INFO: Failure authenticating with NTLM @vmsqldev01:80
AxisFault
faultCode: {http://xml.apache.org/axis/}HTTP
faultSubcode:
faultString: (401)Unauthorized
faultActor:
faultNode:
faultDetail:
{}:return code: 401
{http://xml.apache.org/axis/}HttpErrorCode:401
(401)Unauthorized
at org.apache.axis.transport.http.CommonsHTTPSender.invoke(CommonsHTTPSender.java:218)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)