【问题标题】:Spring Rest template with http client for NTLM authentication用于 NTLM 身份验证的带有 http 客户端的 Spring Rest 模板
【发布时间】:2015-04-24 06:53:47
【问题描述】:

我们在 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身份验证。

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

【问题讨论】:

  • 忽略它。出于某种原因,HttpClient 在协商时将失败记录为警告(恕我直言,应该是信息)。
  • @M.Denium 有没有办法在 HTTPClient 中提供有效的 NTLM 凭据(用户名和密码)并在 spring rest 模板上使用它
  • 你为什么需要它,你声明你得到了回应,因此另一种机制(例如基本)正在工作。您收到警告的事实意味着协商失败(检查响应以检测它支持哪些机制,可能 NTLM 是第一个,基本是第二个)。
  • 感谢您的回复。但只是出于好奇,我想知道如何在 HTTPClient 中传递 NTLM 凭据。这是为了学习目的。如果 Web 服务只支持 NTLM 身份验证,那么我必须知道正确的解决方案...
  • 您在聊天中的讨论不可用。使用 NTCredentials 时您做了什么来解决身份验证错误

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


【解决方案1】:

只需定义以下类。

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

【讨论】:

    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多