How to set NTLM authentication in rest template Header in Spring

假如想象 提交于 2019-12-22 17:20:46

问题


I want to authenticate NTLM using Rest template , can any one suggest the way ?


回答1:


this is what I did taking cues from here. Credits goes here only.

  1. Set up rest template to use apache http client -> compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5'
  2. Updated my rest template bean to use httpclient -

    RestTemplate restTemplate = new RestTemplate(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); restTemplate.setRequestFactory(requestFactory);

  3. Then just do what the link here says. Add the NtlmAuthenticator class and do this just before your restTemplate call.

    NtlmAuthenticator authenticator = new NtlmAuthenticator(userName, password); Authenticator.setDefault(authenticator);

That's it. You are all set up.




回答2:


If anyone stumble upon this entry again, this is the builtin solution:

Ensure your project includes the org.apache.httpcomponents.httpclient.

Then you can build your RestTemplate with this snippet:

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, password, "source-host-name", "domain-name"));
CloseableHttpClient httpClient = HttpClients.custom()
        .setDefaultCredentialsProvider(credsProvider)
        .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);


来源:https://stackoverflow.com/questions/45811242/how-to-set-ntlm-authentication-in-rest-template-header-in-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!