带有http客户端的Spring Rest模板,用于NTLM身份验证

时间:2015-04-24 06:53:47

标签: spring rest apache-httpclient-4.x

我们在IIS服务器中部署了一个基于NTLM身份验证进行身份验证的Web服务。

当我尝试通过在httpCleint UserNamePasswordCredentials中传递用户名和密码来访问Web服务时,我收到警告

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

请说明如何使用Spring rest模板使用http客户端通过用户名和密码传递NTLM身份验证。

注意:虽然收到了警告信息,但我也得到了回复。

1 个答案:

答案 0 :(得分:3)

只需定义以下类。

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

然后添加以下代码。发现它。它开始工作了。

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