While RestTemplate
can be configured to use Apache HttpClient it uses the java.net
classes by default (e.g. URLConnection
).
The following code is untested but "should work"
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());
}
}