问题描述
我们在 IIS 服务器中部署了一个基于 NTLM 身份验证的 Web 服务.
We have a web service deployed in IIS server which authenticate based on NTLM authentication.
当我尝试通过在 httpCleint UserNamePasswordCredentials 中传递用户名和密码来访问 Web 服务时,收到警告为
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
请说明如何使用spring rest模板使用http客户端通过用户名和密码通过NTLM认证.
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.
推荐答案
只需定义以下类即可.
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);
这篇关于带有用于 NTLM 身份验证的 http 客户端的 Spring Rest 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!