Authenticating Backend-to-Backend Services in Spring Boot
A service calling another service needs an identity, permission to perform the requested operation, and a protected transport. Those are related but separate concerns. TLS protects the connection; authentication identifies the caller; authorization decides what that caller may do.
This guide compares common approaches, then shows a Spring Security resource-server configuration. The example targets Spring Boot 3.x and Spring Security 6.x with Java 17 or later. Use the dependency versions managed by your supported Spring Boot release. Older examples based on WebSecurityConfigurerAdapter, ResourceServerConfigurerAdapter and hand-written JWT filters should not be copied into a modern application.
Choosing an approach
| Approach | Useful when | Main trade-off |
|---|---|---|
| HTTP Basic over TLS | A small integration explicitly supports it | A long-lived password is sent on every request; storage and rotation need care. |
| API key over TLS | A provider supports a service-specific key | Usually a long-lived bearer secret; scope, revocation and rotation depend on the provider. |
| OAuth 2.0 client credentials | A confidential service calls a protected API as itself | Requires an authorization server and correct token validation. |
| Mutual TLS | Workload identities and certificate lifecycle are managed | Certificate issuance, rotation, trust and proxy boundaries add operational work. |
| Platform workload identity | Both ends support a cloud or cluster identity system | Strong integration, but the identity and authorization model is platform-specific. |
None is a replacement for endpoint-level authorization. A valid caller should receive only the permissions it needs.
Basic authentication
The server decodes the Authorization header and validates the supplied credentials. Base64 encoding is not encryption; use TLS and protect the long-lived password.

OAuth client credentials: the usual shape
- The calling service authenticates to an authorization server using an approved client-authentication method.
- It requests an access token with the permissions needed for the receiving API.
- It sends that token as
Authorization: Bearer <access-token>over TLS. - The receiving service validates the token and checks the required scope before handling the request.
Client credentials represents the service, not an end user. Do not use it when the receiving API needs the user's delegated permissions. JWT is a token format, while OAuth is a protocol; OAuth access tokens can also be opaque.

The JWT sketch is abbreviated: the receiving API must also validate audience and enforce authorization. For opaque tokens, an authorization server may expose an introspection endpoint instead of local JWT validation:

The configuration below uses JWT validation, not introspection. The client-authentication method at the token endpoint depends on the provider; do not assume every server accepts credentials in the request body.
Receiving service: validate with Spring Security
Add the resource-server starter to a Maven project that already uses Spring Boot dependency management:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>Configure a real issuer and the audience assigned to this API. The addresses below are placeholders, not a working identity provider:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://identity.example.com/issuer
audiences: https://orders.example.comWith a supported Boot version that exposes the audiences property, this checks the issuer and intended audience. Spring's JWT resource server also validates the signature and time-based claims such as expiry. The issuer must expose suitable discovery metadata and keys. Check your Boot version's configuration reference; if it does not support audiences, add an explicit audience validator rather than omitting that check.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class ApiSecurity {
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
return http
// Appropriate only for this stateless, bearer-token-only API.
// Revisit CSRF protection if cookies or browser credentials are added.
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/orders/**")
.hasAuthority("SCOPE_orders.read")
.anyRequest().denyAll())
.oauth2ResourceServer(oauth -> oauth.jwt(Customizer.withDefaults()))
.build();
}
}By default, scopes from the token become authorities prefixed with SCOPE_. An authentic token without orders.read must not grant access to these GET endpoints. This example intentionally denies every other route: add explicit rules for your API rather than accidentally leaving an endpoint open.
Do not replace this with a filter that merely parses a JWT. Parsing is not signature validation, and successful validation alone does not establish a Spring Security authentication or authorize a request.
Calling service: let the OAuth client manage tokens
Use Spring Security's OAuth client support rather than creating a custom token cache or hardcoding a signing key. A client-credentials registration can look like this:
spring:
security:
oauth2:
client:
registration:
orders-client:
provider: company-identity
client-id: ${ORDERS_CLIENT_ID}
client-secret: ${ORDERS_CLIENT_SECRET}
authorization-grant-type: client_credentials
scope: orders.read
provider:
company-identity:
token-uri: https://identity.example.com/oauth2/tokenThis is registration configuration, not a complete outgoing HTTP client. Wire the registration into the authorized-client manager and HTTP client supported by your Spring Security version, and use a stable service principal for this machine-to-machine flow. The Spring authorized-client documentation covers the version-specific client integration.
Keep the secret outside source control, restrict who can read it, and arrange rotation. Where supported, prefer workload identity or asymmetric client authentication to distributing another shared secret. A client-credentials client normally obtains a new access token when needed; it should not assume a refresh token will be issued.
API keys: a narrower option
If a provider requires an API key, send it in its documented header over TLS. Avoid query-string keys: URLs can leak through logs, monitoring and copied links. Avoid logging credentials or tokens, including in error paths.
Do not assume an API-management product's key is an authentication mechanism. AWS explicitly says API Gateway REST API usage-plan keys should not be used for authentication or authorization. They support usage tracking and throttling; use an appropriate authorizer or IAM for access control.

Mutual TLS and platform identity
Mutual TLS authenticates certificates on both sides of the TLS connection. It does not mean that no credential material travels over the network: certificates are exchanged, while private keys remain private. The receiving system must map the verified certificate identity to authorization rules. If TLS terminates at a proxy, establish how verified identity reaches the application without accepting caller-forged headers.

Cloud identity is another option. For example, use AWS IAM and the supported request-signing flow when calling an AWS service or an IAM-protected API. STS temporary credentials do not by themselves validate an arbitrary application's bearer token; identity verification must match the receiving API's actual protocol.
Gateways and workload identity
A gateway can centralize authentication, but downstream authorization and a trusted identity-propagation boundary still matter. The gateway must not let callers forge trusted identity headers.


The IAM sketch is conceptual. A platform may issue temporary credentials used to sign requests rather than an arbitrary bearer token. For AWS IAM-protected APIs, use the documented SigV4 signing flow and matching server-side authorization; do not treat STS as a general JWT validator.

Before production
Test at least: missing token, malformed token, bad signature, expired token, wrong issuer, wrong audience, missing scope, allowed scope, and key rotation. Check timeouts and failure behavior when identity infrastructure is unavailable. Use short-lived tokens, least privilege, TLS, secret rotation and credential-safe logs.
These snippets are a starting point, not a complete deployed system. The correct design also depends on network boundaries, identity-provider policy, deployment topology and the consequences of a stolen credential.