我必须使用Spring RestTemplate服务调用 NTLM身份验证(我认为与 Kerberos 相同)的服务。有人知道我该怎么做吗?
P.D:对不起我的英文。
...谢谢
答案 0 :(得分:1)
虽然可以将RestTemplate
配置为使用Apache HttpClient,但默认情况下它使用java.net
类(例如URLConnection
)。
以下代码未经测试但“应该正常”
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
// maybe use "domain\username" instead
Authenticator.setDefault(new MyAuthenticator("username", "password"));
...
<your-RestTemplate-call-here>
...
class MyAuthenticator extends Authenticator {
private String httpUsername;
private String httpPassword;
public MyAuthenticator(String httpUsername, String httpPassword) {
this.httpUsername = httpUsername;
this.httpPassword = httpPassword;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Scheme:" + getRequestingScheme());
return new PasswordAuthentication(httpUsername, httpPassword.toCharArray());
}
}