Spring Rest template with http client for NTLM aut

2019-08-04 04:50发布

问题:

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 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);