سؤال

I have to call to a service with NTLM authentication (I think is the same as Kerberos) with Spring RestTemplate services. Someone knows how I could do that?

P.D: Sorry for my english.

Thanks...

هل كانت مفيدة؟

المحلول

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());
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top