Taken from the example on this site, I think this would be the most natural way of doing it, by filling in the header value and passing the header to the template.

This is to fill in the header Authorization:

String plainCreds = "willie:p@ssword";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);

And this is to pass the header to the REST template:

HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url, HttpMethod.GET, request, Account.class);
Account account = response.getBody();
Answer from Angular University on Stack Overflow
🌐
Baeldung
baeldung.com › home › spring › spring web › basic authentication with the resttemplate
Basic Authentication with the RestTemplate | Baeldung
December 25, 2023 - Let’s start setting things up with HttpClient 4 and Spring 4. The RestTemplate will require an HTTP request factory that supports Basic Authentication.
🌐
LinkedIn
linkedin.com › pulse › how-use-basic-authentication-rest-template-spring-boot-arth-soni-uglof
How to use Basic Authentication with Rest Template in Spring Boot
February 22, 2024 - We will create a class RestClient and that will call our APIs while building Basic Authentication. While using RestTemplate that Spring Boot provides, you need to pass HttpHeaders with a RequestEntity.
🌐
Medium
abhis2007.medium.com › basic-authentication-in-spring-boot-80a01112f89c
Implementing Basic Authentication with Spring Security - Abhishek Kumar | Medium
May 12, 2024 - Suppose we have built some API endpoints and we found some of the endpoints need to be secure hence the user is supposed to authenticate themself using a login ID and password, here Spring Boot basic authentication will come into the rescue, by default all the endpoints need will be prompted to validated before receiving the response from the API.
🌐
Spring Framework Guru
springframework.guru › home › implementing http basic authentication in a spring boot rest api
Implementing HTTP Basic Authentication in a Spring Boot REST API - Spring Framework Guru
January 5, 2021 - Spring Boot REST APIs have different types of clients accessing from different locations. At times, these APIs need to perform tasks to generate and share sensitive data. Now all clients should not get access to such data, but only a privileged set of clients should. In such scenarios, you need to secure your REST API. One approached to secure REST API is using HTTP basic authentication.
🌐
Elastic
elastic.co › guide › en › elasticsearch › client › java-rest › 6.8 › _basic_authentication.html
Basic authentication | Java REST Client [6.8] | Elastic
Configuring basic authentication can be done by providing an HttpClientConfigCallback while building the RestClient through its builder. The interface has one method that receives an instance of org.apache.http.impl.nio.client.HttpAsyncClientBuilder ...
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot 2 › spring boot resttemplate › basic auth with spring resttemplate
Basic Auth with Spring RestTemplate
January 28, 2022 - In this spring resttemplate example, we learned to pass basic authentication via “Authorization” header while accessing rest api. It is done in two steps. First step is to include required dependencies e.g. spring-boot-starter-web and httpclient.
🌐
Baeldung
baeldung.com › home › http client-side › java httpclient basic authentication
Java HttpClient Basic Authentication | Baeldung
May 20, 2026 - It begins with the Basic keyword, followed by a base64-encoded value of username:password. The colon character is important here. The header should strictly follow this format. For example, to authenticate with baeldung username and HttpClient ...
Find elsewhere
🌐
Terasolunaorg
terasolunaorg.github.io › guideline › 5.3.0.RELEASE › en › ArchitectureInDetail › WebServiceDetail › RestClient.html
5.2. REST Client (HTTP Client) — TERASOLUNA Server Framework for Java (5.x) Development Guideline 5.3.0.RELEASE documentation
Implementation example of request header configuration process for Basic authentication · package com.example.restclient; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; public class BasicAuthInterceptor implements ClientHttpRequestInterceptor { //(1) private static final Logger log = LoggerFactory.getLogger(BasicAuthInterceptor.class); @Value("${api.auth.userid}") String userid; @Value("${api.auth.passw
🌐
Medium
medium.com › javarevisited › spring-boot-securing-api-with-basic-authentication-bdd3ad2266f5
Spring Boot — Securing API with basic authentication | by Daily Debug | Javarevisited | Medium
August 17, 2024 - https://medium.com/@dailydebug... mechanism, it is part of HTTP specification and involves sending a username and password encoded in the HTTP request header, it is a straightforward mechanism to provide security to ...
🌐
Java Guides
javaguides.net › 2023 › 11 › spring-boot-webclient-basic-authentication.html
Spring Boot WebClient Basic Authentication
November 8, 2023 - To consume the secured REST API with the WebClient, you need to set up your WebClient with basic authentication headers. In this tutorial, we will see how to create a Spring Boot application that sets up WebClient to consume the /greeting endpoint of a REST API secured with Basic Authentication.Spring's WebClient is a modern, non-blocking, and reactive client for HTTP requests.
🌐
Theautonomy
theautonomy.github.io › articles › spring-restclient-guide.html
Spring RestClient: A Modern Approach to HTTP Communication
One of the most powerful features of RestClient is support for declarative HTTP interfaces using @HttpExchange annotations. ... import org.springframework.web.service.annotation.*; @HttpExchange public interface UserClient { @GetExchange("/users") List<User> getAllUsers(); @GetExchange("/users/{id}") User getUser(@PathVariable Long id); @PostExchange("/users") User createUser(@RequestBody User user); @PutExchange("/users/{id}") User updateUser(@PathVariable Long id, @RequestBody User user); @DeleteExchange("/users/{id}") void deleteUser(@PathVariable Long id); @GetExchange("/users/search") List<User> searchUsers(@RequestParam String name, @RequestParam(required = false) Integer age); }
🌐
Medium
medium.com › @aldisti0 › how-to-make-oauth2-authenticated-requests-in-spring-boot-3-with-restclient-37b502429f8d
How to make OAuth2 authenticated requests in Spring Boot 3 with RestClient | by Aldisti | Medium
September 3, 2025 - This article has outlined the process of configuring an OAuth2 client in Spring Security 6, obtaining an AuthorizeClient from within an Interceptor, and subsequently authenticating all requests issued by a RestClient.
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot 2 › securing spring boot rest api with basic auth
Securing Spring Boot REST API with Basic Auth
December 9, 2022 - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> To enable authentication and authorization support, we can configure the utility class WebSecurityConfigurerAdapter (deprecated). It helps in requiring the user to be authenticated prior to accessing any configured URL (or all URLs) within our application. In the following configuration, we are using httpBasic() which enables basic authentication.
🌐
Spring
spring.io › blog › 2024 › 10 › 28 › restclient-support-for-oauth2-in-spring-security-6-4
RestClient Support for OAuth2 in Spring Security 6.4
October 28, 2024 - Spring Security provides implementations of OAuth2AuthorizedClientManager, which is a component that can be used to obtain access tokens (such as a JWT). An instance of this component is automatically published as a bean by Spring Security, meaning we simply need to inject it into our own configuration in order to set up a RestClient for making protected resources requests in our application.
🌐
GitHub
github.com › sophea › spring-rest-api-basic-auth
GitHub - sophea/spring-rest-api-basic-auth: Spring REST-API with Spring Basic Authentication type · GitHub
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName() + ""); PrintWriter writer = response.getWriter(); writer.println("HTTP Status 401 : " + authException.getMessage()); } @Override public void afterPropertiesSet() throws Exception { setRealmName(SecurityConfiguration.REALM); super.afterPropertiesSet(); } } =========================================================== ref : http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/
Forked by 2 users
Languages: Java
🌐
ITNEXT
itnext.io › authentication-with-microprofile-rest-client-d1e9da774f70
(Basic) Authentication with MicroProfile Rest-Client | by Heiko W. Rupp | ITNEXT
November 20, 2019 - @Path("/api/verify") @RegisterRestClient @ClientHeaderParam(name = "Authorization", value = "{lookupAuth}") public interface VerifyEngine { default String lookupAuth() { return "Basic " + Base64.getEncoder().encodeToString("user:pass".getBytes()); }
🌐
Stack Overflow
stackoverflow.com › questions › 47924356 › spring-security-basic-authorization-for-rest-client
java - Spring Security Basic Authorization for REST Client - Stack Overflow
December 21, 2017 - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName() + ""); PrintWriter writer = response.getWriter(); writer.println("HTTP Status 401 : " + authException.getMessage()); } @Override public void afterPropertiesSet() throws Exception { setRealmName("Coma-Frontend"); super.afterPropertiesSet(); } } ... seems to me you have csrf enabled., since you want to hit you api from another spring boot app this one not being an html form.