title: Spring Boot RestTemplate Basic Authentication using RestTemplateBuilder
body: <p>In Spring Boot I'm trying to create a RestTemplate which will use basic authentication using </p>
  
  <pre><code>@Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
      builder.basicAuthorization("username", "password");
      RestTemplate template = builder.build();
      return template;
  }
  </code></pre>
  
  <p>I then inject the RestTemplate in my service class as</p>
  
  <pre><code>@Autowired
  private RestTemplate restTemplate;
  </code></pre>
  
  <p>However, my requests fail with a 401 unauthorized exception:</p>
  
  <pre><code>Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
  </code></pre>
  
  <p>Using another REST Client (Postman) the requests to the same URL succeeds so I assume the basic authentication is not working correctly. From the debug output it looks as if the authentication header is not being set. What will make this work?</p>