Video in progress

How do you pass the user's identity between microservices, and how do services recognize each other?

The user's JWT is validated at the gateway and passed on to internal calls, and each service validates it itself as a resource server. For background jobs the service obtains a client credentials token in its own name.

📖 Answer

You have to distinguish two situations.

A call on behalf of a user. The gateway validates the token, but internal services shouldn't trust it blindly: if someone gets into the internal network, they can write the X-User-Id: 1 header themselves. So each service checks the JWT itself, which is cheap: the signature is verified locally with the JWKS keys.

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://auth.example.uz/realms/shop

On the call to the next service the token is passed in the Authorization header.

A call in the service's own name, for example a nightly report job. There's no user here. The service gets its own token with the OAuth2 client_credentials grant, carrying a scope or role like reports:read. In Spring, OAuth2AuthorizedClientManager handles obtaining and refreshing the token.

As an extra layer, mTLS is used, often through a service mesh (Istio, Linkerd), without touching the code. It guarantees that "this connection really is from order-service", but it doesn't answer "which user", which is why you still need the token.