Spring 4.0.0 basic authentication with RestTemplate

前端 未结 6 1735
梦谈多话
梦谈多话 2020-12-13 10:37

I am currently working on integration of a third party application with our local reporting system. I would like to implement REST calls with basic authentication but facing

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 11:13

    I know that this is an old question, but I was looking for the answer to this myself. You need to add a RestTemplate interceptor when configuring the RestTemplate. An example below in annotation configuration:

    @Bean
    public RestTemplate restTemplate() {
    
        final RestTemplate restTemplate = new RestTemplate();
    
        restTemplate.setMessageConverters(Arrays.asList(
                new FormHttpMessageConverter(),
                new StringHttpMessageConverter()
        ));
        restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("client", "secret"));
    
        return restTemplate;
    }
    

    Javadoc for BasicAuthorizationInterceptor.

    I was stuck on this for a good few hours. Maybe it will help somebody out in the near future.

提交回复
热议问题