How does Service-to-Service Authentication work in microservices?
Topic: microservices, Security. Level: Hard.
Answer
Internal calls must authenticate the *caller service*, not just end users. Common approaches: **mTLS** (each service presents a certificate; identity is the cert, often automated by a service mesh), **OAuth2 client-credentials** (a service gets a token from an auth server and presents it as a Bearer JWT), and **signed JWTs** propagating the end-user context downstream. Verify tokens' signature, issuer, audience, and expiry on every hop; apply least-privilege scopes. Never trust the network alone (zero-trust).
```java
// Caller obtains a client-credentials token, forwards it as Bearer
String token = oauth.clientCredentialsToken("order-service", scopes("payment.charge"));
paymentClient.post().uri("/charge")
.header("Authorization", "Bearer " + token)
.body(req).retrieve();
// Callee validates issuer + audience + scope on the JWT (Spring Security)
http.oauth2ResourceServer(o -> o.jwt(j -> j.jwtAuthenticationConverter(scopeConverter)));
```