You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by $tamo$ <ss...@yahoo.com> on 2008/06/14 11:23:33 UTC

NTLM, client stubs, UsernamePasswordCredentials vs NTCredentials

Hi,

Server side:
MS SQL server 2005, SOAP endpoints -> web services using NTLM authentication.
resulting wsdl: polos.wsdl (attachment)

Client side:
Axis2 1.4
generated client stubs using:  java org.apache.axis.wsdl.WSDL2Java -v  -a -H -B polos.wsdl
I add a client-config.wsdd in to the jar when I compile the client (Netbeans 6.1):
The content of the wsdd file


<code>
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="commonsHTTPConfig"
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<transport name="http" pivot="java:org.apache.axis.transport.http.CommonsHTTPSender" />
<transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
<transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender" />

</deployment>
</code>


As you can see I use CommonsHTTPSender because i would like to use the NTLM features.
I created a method to do run the client:

<code>
try {

            InergopolosLocator inergopolosLocator = new InergopolosLocator();
            InergopolosSoapStub inergopolosLocatStub = (InergopolosSoapStub) inergopolosLocator.getinergopolos();

            inergopolosLocatStub.setUsername("domain\\user");
            inergopolosLocatStub.setUsername("01234");

            System.out.println(inergopolosLocatStub.storms().toString());
        } catch (Exception ex) {
            System.out.println(ex);
        }
</code>



When I run the client I get the following error:

<code>
run:
- ntlm authentication scheme selected
- Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
org.apache.commons.httpclient.auth.InvalidCredentialsException: Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
        at org.apache.commons.httpclient.auth.NTLMScheme.authenticate(NTLMScheme.java:331)
        at org.apache.commons.httpclient.HttpMethodDirector.authenticateHost(HttpMethodDirector.java:277)
        at org.apache.commons.httpclient.HttpMethodDirector.authenticate(HttpMethodDirector.java:229)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:167)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
        at org.apache.axis.transport.http.CommonsHTTPSender.invoke(CommonsHTTPSender.java:196)
        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)
        at org.tempuri.InergopolosSoapStub.storms(InergopolosSoapStub.java:853)
        ......
        - Failure authenticating with NTLM <any realm>@inergopta:80
        (401)Unauthorized
</code>

As you see NTLM scheme is selected, however instead of the NTCredentials class is used the UsernamePasswordCredentials class that it is inappropriate for NTLM.

The question is: how can I persuade the stub to use the NTCredentials class? should I edit something form the stub? or from the wsdd? or from the method that calls the stub?

Just hit me with any idea! I struggle with it for about 3 weeks (read every single forum post...)
thanks!





P.S.
this code works just fine:

        // Create an instance of HttpClient.
        HttpClient client = new HttpClient();
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new NTCredentials("Administrator","Pa$$w0rd","","");
        // client.getState().setCredentials(new AuthScope("inergopta", 80, AuthScope.ANY_REALM), defaultcreds);
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);
     //   client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);

        // Create a method instance.
        GetMethod method = new GetMethod(url);
        method.setDoAuthentication(true);

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        try {
            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }

            // Read the response body.
            byte[] responseBody = method.getResponseBody();

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
            System.out.println(new String(responseBody));

        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Release the connection.
            method.releaseConnection();
        }