我们在 IIS 服务器中部署了一个 Web 服务,该服务基于 NTLM 身份验证进行身份验证。
当我尝试通过在 httpClient UserNamePasswordCredentials 中传递用户名和密码来访问 Web 服务时,收到警告:
NTLM authentication error: Credentials cannot be used for NTLM authentication: org.apache.http.auth.UsernamePasswordCredentials
请说明如何使用 Spring Rest 模板使用 http 客户端来通过用户名和密码进行 NTLM 身份验证。
注意:虽然我收到警告消息,但也收到回复。
只需定义以下类即可。
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)