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

Ren*_*n V 5 rest spring apache-httpclient-4.x

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

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

NTLM authentication error: Credentials cannot be used for NTLM authentication: org.apache.http.auth.UsernamePasswordCredentials
Run Code Online (Sandbox Code Playgroud)

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

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

Ren*_*n V 5

只需定义以下类。

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));
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后添加以下代码。就是这样。它开始工作了。

NtlmAuthenticator authenticator = new NtlmAuthenticator(userName,
                    password);
            Authenticator.setDefault(authenticator);
Run Code Online (Sandbox Code Playgroud)