Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Rest template with http client for NTLM authentication

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.

like image 202
Renganathan V Avatar asked Apr 24 '15 06:04

Renganathan V


1 Answers

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);
like image 186
Renganathan V Avatar answered Oct 29 '22 15:10

Renganathan V