5
votes

We have a web service deployed in IIS server which authenticate based on NTLM authentication.

When i try to access the web service by passing username and password in httpCleint UserNamePasswordCredentials, am getting warnings as

NTLM authentication error: Credentials cannot be used for NTLM authentication: org.apache.http.auth.UsernamePasswordCredentials

Please clarify how to user http client with spring rest template to pass the NTLM authentication with user name and password.

Note:Though am getting the warning message, am also getting response.

1
Just ignore it. The HttpClient, for some reason, logs the failures when negotiating as warning (imho it should be info). - M. Deinum
@M.Denium is there any way to provide valid NTLM credentials(username and pwd) in HTTPClient and use it over spring rest template - Renganathan V
Why would you need that, you state that you get a response, hence another mechanism (basic for instance) is working. The fact that you get a warning means that that negotiation failed (check the response to detect which mechanisms it supports, probably NTLM is the first, basic the second). - M. Deinum
Thanks for your reply. But just for my curiosity, I want to know how to pass NTLM credentials in HTTPClient. This is for learning purpose. In case, if web service supports only NTLM authentication, then I have to know solution right... - Renganathan V
Your discussion in chat is unavailable. What did you do to resolve Authentication error when using NTCredentials - DolphinJava

1 Answers

5
votes

Just define the following class.

public class NtlmAuthenticator extends Authenticator {

        private final String username;
        private final char[] password;

        public NtlmAuthenticator(final String username, final String password) {
            super();
            this.username = username;
            this.password = password.toCharArray();
        }

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(username, password));
        }
    }

then add the following code.Thats it.It started working.

NtlmAuthenticator authenticator = new NtlmAuthenticator(userName,
                    password);
            Authenticator.setDefault(authenticator);