문제

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