Search code examples
javaauthenticationhttpclientkerberosjava-17

Kerberos auth with Apache httpcomponents (httpclient5)


I want to migrate from Apache httpclient (v3.x and Java 8) to httpcomponents (httpclient5 and Java 17) and move away from my NTLM auth to Kerberos. I have not found any example though on how to do this. I am only finding examples of v4.x and below. Any ideas how to do this in v5.x? Is it even possible?

A rough version of my code currently:

CredentialsProvider credsProvider;
RequestConfig requestConfig;
credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, pass, host, domain));
requestConfig = RequestConfig.custom().setTargetPreferredAuthSchemes(Collections.singletonList(AuthSchemes.NTLM)).build();
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
                .setDefaultRequestConfig(requestConfig).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.getForObject(url, String.class);

Solution

  • I ended up going for the Spring based approach (it is a Spring project either way), which is cleaner and easier, as seen in this simple example.